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

问题描述 投票:0回答:1

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

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

现在,我想添加旧URL以便突出显示。而不是链接,我有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属性)。

angularjs angularjs-filter
1个回答
0
投票

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

angular.forEach(this.$filter('filter')(this.fullNav, (item) => {
    return Array.isArray(item.links) && item.links.includes(this.$location.$$path);
}, true), (item) => {
    item.active = true;
});
© www.soinside.com 2019 - 2024. All rights reserved.