AngularJs删除在NG-重复重复元素
我有存储在 field_detail
<li ng-repeat = "field in field_detail">{{field.displayName}}</li>
现在我不想有重复的显示名
field_detail
,是什么过滤器
我应该使用?
Now I dont want to include duplicate displayName
of field_detail
, what filter
should I use?
只要创建一个过滤器,得到独特的价值观,可能是一个关键。像这样的东西应该做的(我没有测试这个好,所以我会离开,企业给你,这只是给你一个想法):
Just create a filter that gets the unique values, probably by a key. Something like this ought to do (I didn't test this at all, so I'll leave that business to you, this is just to give you an idea):
app.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
});
<div ng-repeat="item in items | unique: 'id'"></div>
注意:Array.indexOf()在IE8 不起作用,所以如果你支持IE8,你需要的indexOf()存根,否则你会需要使用略有不同的方法。
Note: Array.indexOf() does not work in IE8, so if you're supporting IE8, you'll need to stub in indexOf(), or you'll need to use a slightly different approach.
其他的想法:这可能只是为了更好地创建一个过滤器,它利用独特的功能lowdash或下划线,如果您已经在引用这些库
Other thoughts: It's probably better just to create a filter that leverages the unique function in lowdash or underscore if you're already referencing those libraries.