1

I have a search box using angular:

<input type="text"
typeahead="item.name for item in list |
filter: $viewValue:comparator | orderBy: '+name'"/>

I want to add another filter to exclude certain items from another array. Something like:

... | exclude: items in excludedList

I don't want to use ng-hide; I'm specifically looking for a filter to do this.

1 Answer 1

2

This look like a job for a custom filter:

.filter('excludeItemsByName', function () {
        return function (items, excludedList) {
            var ret = [];
            angular.forEach(items, function (item) {
                if (excludedList.indexOf(item.name) === -1) {
                    ret.push(item);
                }
            });
            // or you could use Array.prototype.filter() in browsers that support it
            return ret;
        };
    })

Then

<input type="text"
typeahead="item.name for item in list |
filter: $viewValue:comparator | excludeItemsByName:['excludeThis','excludeThat'] orderBy: '+name'"/>

(This assumes excludedList will be a flat list of strings; it should be clear enough how to extend this to support excluded objects instead...)

https://docs.angularjs.org/guide/filter#creating-custom-filters

Sign up to request clarification or add additional context in comments.

2 Comments

This might fail in case of array of objects. Right?
@manu29.d As always, it depends on if you're expecting an exclusion by reference or by equality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.