简体   繁体   中英

Filtering ng-options

Prior to updating Angular to 1.3, I was using 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).

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.

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. I do not know what is happening.

EDIT: Added 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. Try comparing the code from 1.2.13 and 1.3.13 of AngularJS.

1.3.13 throws a bunch of infdig errors.

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

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 );
        });

    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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