简体   繁体   English

过滤ng-options

[英]Filtering ng-options

Prior to updating Angular to 1.3, I was using Angular 1.2. 在将Angular更新到1.3之前,我使用的是Angular 1.2。

I was filtering my list of categories using this filter. 我正在使用此过滤器过滤类别列表。 What it does is remove a category that is a duplicate of what is passed in (me). 它所做的是删除一个类别,该类别与(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;
    };
});

Using this in my HTML, everything worked dandy. 在我的HTML中使用此功能,一切正常。

ng-options="c as c.name for cat in filtered = (cats | CategorySelectFilter: cat) track by cat.id"

After updating to Angular 1.3, I started getting Infdig errors from this. 更新到Angular 1.3之后,我开始从中收到Infdig错误。 I do not know what is happening. 我不知道发生了什么。

EDIT: Added Plnkr http://plnkr.co/edit/X2cAvRyd3pdjMDOOQKfI?p=preview 编辑:添加了Plnkr http://plnkr.co/edit/X2cAvRyd3pdjMDOOQKfI?p=preview

It seems to work fine in the Plnkr code, but still getting Infdig errors from the console. 在Plnkr代码中似乎可以正常工作,但仍会从控制台收到Infdig错误。 Try comparing the code from 1.2.13 and 1.3.13 of AngularJS. 尝试比较AngularJS的1.2.13和1.3.13中的代码。

1.3.13 throws a bunch of infdig errors. 1.3.13会引发大量入侵错误。

This answer provides some cool filters to look at . 此答案提供了一些很酷的过滤器供您查看 If you are doing a lot of filtering maybe its easy enough to import those libs. 如果您要进行大量过滤,也许导入这些库很容易。

I created a fiddle to solve your issue. 我创建了一个小提琴来解决您的问题。

or edited your plunker http://plnkr.co/edit/AI1O9Z?p=preview 或编辑您的监听器http://plnkr.co/edit/AI1O9Z?p=preview

this is how to use it. 这是如何使用它。

 ng-repeat="cat in cats | catFilter:'id' "

and the filter. 和过滤器。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM