我在angularJS中有以下过滤器:
<div ng-repeat="node in data | filter:{parentID:12}"></div>
Run Code Online (Sandbox Code Playgroud)
这工作正常(我只得到数据,其中parentID是12).
在下一步中,我想获取所有数据,其中parentID是(例如)12,13,25或30.
我试过跟随(它不工作):
filter:{parentID:[12,13,25,30]}
Run Code Online (Sandbox Code Playgroud)
有没有办法按照描述构建过滤器?
非常感谢你!
谓词函数可以很好地满足您的需求!来自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.
Run Code Online (Sandbox Code Playgroud)
例如:
<div ng-repeat="node in data | filter:checkParentID"></div>
Run Code Online (Sandbox Code Playgroud)
并在你的控制器
$scope.checkParentID = function(value, index) {
return value.parentID && [12,13,25,30].indexOf(value.parentID) !== -1;
}
Run Code Online (Sandbox Code Playgroud)