hashes = [ { "type": "foo",… },{ "type": "foo",{ "type": "bar",… } ]
如果它的值与针列表中的项匹配,我想根据类型过滤它们:@H_502_4@
types = ['foo','quz'];
所以像@H_502_4@
<div ng-repeat="hash in hashes | filter:types"></div>
hashes = [ { "type": "foo",… },{ "type": "foo",{ "type": "bar",… } ]
如果它的值与针列表中的项匹配,我想根据类型过滤它们:@H_502_4@
types = ['foo','quz'];
所以像@H_502_4@
<div ng-repeat="hash in hashes | filter:types"></div>
<div ng-repeat="hash in hashes | filter: {type:'foo'}">
要过滤数组,您不需要完全自定义的过滤器,但我会使用谓词过滤器,您可以将其传递给Angular的过滤器.这是过滤器,假设您的数组类型:@H_502_4@
$scope.filterArray = function(hash) { return ($scope.types.indexOf(hash.type) !== -1); };
像这样使用:@H_502_4@
<div ng-repeat="hash in hashes | filter: filterArray">
两者都是demo fiddle@H_502_4@
自订过滤器@H_502_4@
filter('inArray',function() { return function inArray( haystack,needle ) { var result = []; var item,i; for (i=0; i< haystack.length;i++) { item = haystack[i]; if (needle.indexOf(item.type) !== -1) result.push(item); }; return (result); }; });
像这样使用:@H_502_4@
<div ng-repeat="hash in hashes | inArray: types">
demo of custom filter@H_502_4@