问题
I'm experimenting with AngularJS for the first time. I do repeat a template based on JSON data, which here is a sample:
$scope.users = [
{name: 'first user', status: {name: 'employee'}},
{name: 'second user', status: {name: 'freelancer'}},
{name: 'third user', status: {name: 'employee'}},
];
This works fine:
<p ng-repeat="user in users">{{user.name}}</p>
Now I want to prefilter the users displayed. Works fine, too:
<p ng-repeat="user in users | filter:{status: 'employee'}">{{user.name}}</p>
But when I want to filter on the basis of JSON data within a nested object (status.name instead of status), it does not work anymore!
<p ng-repeat="user in users | filter:{status.name: 'employee'}">{{user.name}}</p>
I'm using AngularJS 1.2.18. If I use an older version, like AngularJS 1.2.0-rc.3, it works again.
I can't find any information in the documentation about that behaviour. Is there a new syntax or is the feature just not implemented anymore?
回答1:
It's a breaking change introduces in AngularJS 1.2.11. It has been decided to not mark it as such, because it was not a tested nor a documented behaviour.
Since this release, myObject | filter:{'key.subkey':'search'} will search the string search into myObject['key.subkey'] and no more in myObject['key']['subkey'].
A new syntax was introduced in AngularJS 1.2.13, which seems to me more natural, to search in nested object: myObject | filter:{key : {subkey : 'search'}}. That's the solution you're looking for.
<p ng-repeat="user in users | filter:{status : {name: 'employee'}}">{{user.name}}</p>
来源:https://stackoverflow.com/questions/24266784/nested-filtering-with-angular-js-version-1-2-18