AngularJS:对过滤器使用数组(多个值)

时间:2022-12-29 20:13:45

I have following filter in angularJS:

我在angularJS中有以下过滤器:

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

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

这工作正常(我只得到数据,其中parentID是12)。

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

在下一步中,我想获取所有数据,其中parentID是(例如)12,13,25或30。

I tried following (its not working):

我试过跟随(它不工作):

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

Is there any way to build a filter as described?

有没有办法按照描述构建过滤器?

Thank you very much!

非常感谢你!

1 个解决方案

#1


6  

A predicate function will suit your needs nicely! From the 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 example:

<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;
}

#1


6  

A predicate function will suit your needs nicely! From the 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 example:

<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;
}