0
有一个数组(项目数组)一组对象的具有财产
item_id:
[{item_id:1,...},{item_id:2,...}...]
有一组
item_ids:
[2, 8, 10]
如何使用angularjs的$过滤器,以项目组,其中item_id
匹配那些item_ids
阵列获取的对象数组另一个数组。
有一个数组(项目数组)一组对象的具有财产
item_id:
[{item_id:1,...},{item_id:2,...}...]
有一组
item_ids:
[2, 8, 10]
如何使用angularjs的$过滤器,以项目组,其中item_id
匹配那些item_ids
阵列获取的对象数组另一个数组。
您可以使用自定义过滤器来执行此类过滤。你可以使用$filter
服务,如果你想在代码而不是模板中做到这一点。
请参见下面的代码。
var app = angular.module('app', []);
app.controller('ctrl', function($scope, $filter) {
$scope.items = [{
item_id: 1
}, {
item_id: 2
}, {
item_id: 3
}, {
item_id: 4
}];
$scope.findList = [2, 4];
$scope.findList2 = [3];
// Using $filter service.
$scope.usingservice = $filter('findobj')($scope.items, [1, 3])
});
app.filter('findobj', function() {
return function(list, obj) {
return list.filter(function(l) {
if (obj.indexOf(l.item_id) >= 0) {
return true;
}
});
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<div ng-repeat="item in items | findobj: findList">
{{item}}
</div>
<hr/>
<div ng-repeat="item in items | findobj: findList2">
{{item}}
</div>
<hr/>{{usingservice}}
</div>
</div>
通缉使用$过滤器来做到这一点的代码,如在你的榜样。不知道数组可以用来代替过滤条件。另一部分(使用ng-repeat)也非常有用。 – mssuley