我想匹配规则,利用角过滤器过滤器领域,如下所示: http://plnkr.co/edit/dQiv5lRzhQNjXZ6pVdWO?p=$p$pview
I'm trying to match rules to fields using the 'filter' filter in Angular as shown here: http://plnkr.co/edit/dQiv5lRzhQNjXZ6pVdWO?p=preview
我使用的过滤器,如下所示:
I'm using the filter as shown below:
<div ng-repeat="f in fields">
<h4>{{f.id}}</h4>
<li ng-repeat = "rule in rules | filter:{field: {id: f.id} }">
{{rule.name}}
</li>
</div>
这正常工作与个位数的ID,但是有两个数字,如下图所示:
This works fine with single digit ids, but with two digits, as shown below:
$scope.fields = [{id: 1}, {id: 2}, {id: 3}];
$scope.rules = [{name: "A", field: {id: 12, age: 3}}, {name: "B", field: {id: 2, age: 1}}];
id为12的规则获取与1和2的ID相匹配的领域,当它应该只得到匹配ID 12场有没有办法使用默认过滤器要做到这一点,或者我应该创建一个自定义过滤器?
The rule with id 12 gets matched to the fields with ids of 1 and 2, when it should only get matched to fields of id 12. Is there a way to do this with the default filter, or should I create a custom filter?
您可以创建自己的过滤器:
You can create your own filter:
.filter('byId',function(){
return function(items, id){
var res = [];
for(var i=0;i<items.length;i++){
if(items[i].field.id===id){
res.push(items[i]);
}
}
return res;
};
});
HTML:
<li ng-repeat = "rule in rules | byId:f.id">
{{rule.name}}
</li>
http://plnkr.co/edit/d3jmMWOwsHlMEya2Kjei?p=$p$ PVIEW