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

前端之家收集整理的这篇文章主要介绍了AngularJS:对过滤器使用数组(多个值)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在angularJS中有以下过滤器:

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

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

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

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

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

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

非常感谢你!

解决方法

谓词函数可以很好地满足您的需求!从 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.

例如:

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

并在你的控制器

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

猜你在找的Angularjs相关文章