[英]Filtering ng-options
在将Angular更新到1.3之前,我使用的是Angular 1.2。
我正在使用此过滤器过滤类别列表。 它所做的是删除一个类别,该类别与(me)中传入的内容重复。
app.filter('CategorySelectFilter', function() {
return function(cats, me) {
var filteredCats = angular.copy(cats);
for (var index in filteredCats) {
if (me.id == filteredCats[index].id) {
filteredCats.splice(index, 1);
}
}
return filteredCats;
};
});
在我的HTML中使用此功能,一切正常。
ng-options="c as c.name for cat in filtered = (cats | CategorySelectFilter: cat) track by cat.id"
更新到Angular 1.3之后,我开始从中收到Infdig错误。 我不知道发生了什么。
编辑:添加了Plnkr http://plnkr.co/edit/X2cAvRyd3pdjMDOOQKfI?p=preview
在Plnkr代码中似乎可以正常工作,但仍会从控制台收到Infdig错误。 尝试比较AngularJS的1.2.13和1.3.13中的代码。
1.3.13会引发大量入侵错误。
此答案提供了一些很酷的过滤器供您查看 。 如果您要进行大量过滤,也许导入这些库很容易。
或编辑您的监听器http://plnkr.co/edit/AI1O9Z?p=preview
这是如何使用它。
ng-repeat="cat in cats | catFilter:'id' "
和过滤器。
app.filter('catFilter', function() {
return function(cats, key ) {
return cats.filter( function(elem, index, array ) {
return ( array.map(function(item){return item[key];}).indexOf(elem[key]) === index );
});
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.