How to filter a nested object not equal in Angular?

You may be missing the simplest syntax, but I can't get an unequal filter to work:

I can do

filter: {property:{text:'yes'}} ,

but not

filter: {property:{text:'!yes'}} ,

which works for non-nested objects.

HTML:

 <ul> <li ng-repeat="attr in attributes | filter: {property:{text:'!yes'}}"> {{attr.property.text}} </li> </ul> 

JS:

 $scope.attributes = [ {property: { text:'yes' }}, {property: { text:'no' }}, ]; 

Link Plunkr:

http://plnkr.co/edit/2mTcQijmfnqAM5vUtKsK?p=preview

+5
source share
2 answers

You can get the same effect with ng-if as follows:

 <ul> <li ng-repeat="attr in attributes" ng-if="attr.property.text !== 'yes'"> {{attr.property.text}} </li> </ul> 

Alternatively, you can write your own filter that contains logic or smooth out the original structure in some way. I do not think so ! supported in nested structures.

+6
source

you can use

 filter: !{property:{text:'yes'}} 

This is useful in situations where you cannot use ng-if as

 <select ng-options="element in elementList | filter: !{property: {text:'yes'} }" /> 

(I know you can use ng-repeat in parameters, but you will get my point ...)

+4
source

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


All Articles