angularjsangularjs-filter

AngularJS filter where checking if array parameter contains value


I have simple function to set active state for nav based on url:

angular.forEach(this.$filter('filter')(this.fullNav, {
    'link': this.$location.$$path
}, true), (item) => {
    item.active = true;
});

Now I want to add legacy URLs to be also highlighted. Instead of link I have links (if there are more then one, but I can make all links parameters to be arrays.

To have it work with links and link paramaters I made change to this:

angular.forEach(this.fullNav, (item) => {
    if(item.links) {
        if(item.links.includes(this.$location.$$path)) {
            item.active = true;
        }
    } else {
        if(item.link === this.$location.$$path) {
            item.active = true;
        }
    }
});

How can I write the second function in $filter form? Or at least without if else statement (by removing link property and having only links property).


Solution

  • you can try following code which is similar to the code you have added.

    angular.forEach(this.$filter('filter')(this.fullNav, (item) => {
        return Array.isArray(item.links) && item.links.includes(this.$location.$$path);
    }, true), (item) => {
        item.active = true;
    });