Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter nested object not equal in Angular?

Might be missing some simple syntax, but I can't seem to get not equal filter to work:

I can do

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

but not

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

which does work 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' }},
];

Plunkr link:

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

like image 320
WBC Avatar asked Nov 04 '14 18:11

WBC


1 Answers

You can get the same effect with ng-if like this:

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

Alternatively you could write a custom filter that contains the logic or flatten the original structure somehow. I don't think ! is supported in nested structures.

like image 96
Juho Vepsäläinen Avatar answered Sep 30 '22 22:09

Juho Vepsäläinen