AngularJs How to filter ngRepeat with another array elements

Mo. picture Mo. · Sep 17, 2015 · Viewed 20.5k times · Source

Is there any option to filter from $scope.items where the ID exist in the array $scope.myitems ?

ng-repeat="item in items | filter:{item.id == myitems}

Demo: http://codepen.io/anon/pen/rOeGYB

Answer

Kashif Mustafa picture Kashif Mustafa · Sep 17, 2015

Your problem is that you are trying to get a match on a sub-property of the object(s) you are iterating over.

From the docs:

Note that a named property will match properties on the same level only, while the special $ property will match properties on the same level or deeper. E.g. an array item like {name: {first: 'John', last: 'Doe'}} will not be matched by {name: 'John'}, but will be matched by {$: 'John'}.

I would recommend you will make your custom filter. I have changed your code by implementing custom filter, working copy of your requirement.

<li class="list-group-item" ng-repeat='item in items | customArray:myitems:"id"'>{{item.name}}</li>

See complete plunker here https://codepen.io/anon/pen/PPNJLB