Nested filtering using Angular.js version 1.2.18

I am experimenting with AngularJS for the first time. I am repeating a template based on JSON data, which 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 great:

 <p ng-repeat="user in users">{{user.name}}</p> 

Now I want to pre-filter the displayed users. It works well:

 <p ng-repeat="user in users | filter:{status: 'employee'}">{{user.name}}</p> 

But when I want to filter based on JSON data inside a nested object ( status.name instead of status ), it no longer works!

 <p ng-repeat="user in users | filter:{status.name: 'employee'}">{{user.name}}</p> 

I am using AngularJS 1.2.18. If I use an older version like AngularJS 1.2.0-rc.3, it works again.

I can not find any information in the documentation about this behavior. Is there a new syntax or function is no longer implemented?

+2
source share
1 answer

This is a terrific change in AngularJS 1.2.11 . This was taken not to label it as such, because it was not tested and not documented.

Since this release is myObject | filter:{'key.subkey':'search'} myObject | filter:{'key.subkey':'search'} will search for the search string in myObject['key.subkey'] and no more in myObject['key']['subkey'] .

The new syntax was introduced in AngularJS 1.2.13 , which seems to me more natural for searching in a nested object: myObject | filter:{key : {subkey : 'search'}} myObject | filter:{key : {subkey : 'search'}} . This is the solution you are looking for.

 <p ng-repeat="user in users | filter:{status : {name: 'employee'}}">{{user.name}}</p> 
+3
source

Source: https://habr.com/ru/post/1206144/


All Articles