AngularJS: use array (multiple values) for filter

I have the following filter in angular JS:

<div ng-repeat="node in data | filter:{parentID:12}"></div> 

This works fine (I only get data where parentID is 12).

In the next step, I want to get all the data where the parentID (for example) is 12,13,25 or 30.

I tried (doesn't work):

 filter:{parentID:[12,13,25,30]} 

Is there a way to create a filter as described?

Many thanks!

+5
source share
1 answer

The predicate function will satisfy your needs! From doc :

 function(value, index): A predicate function can be used to write arbitrary filters. The function is called for each element of array. The final result is an array of those elements that the predicate returned true for. 

For instance:

 <div ng-repeat="node in data | filter:checkParentID"></div> 

And in your controller

 $scope.checkParentID = function(value, index) { return value.parentID && [12,13,25,30].indexOf(value.parentID) !== -1; } 
+6
source

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