AngularJS过滤器,用于检查数组参数是否包含值

时间:2020-02-19 09:36:43

标签: angularjs angularjs-filter

我有简单的功能可以根据url设置导航的活动状态:

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

现在,我想添加旧网址以突出显示。除了链接之外,我还有 links (如果有多个链接,则可以,但是我可以将所有links参数设为数组。

要使其与链接和链接参数一起使用,我对此进行了更改:

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

如何以$ filter形式编写第二个函数?或至少没有if else语句(通过删除 link 属性并仅具有 links 属性)。

1 个答案:

答案 0 :(得分:1)

您可以尝试遵循与添加的代码相似的代码。

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