【问题标题】:AngularJS filter where checking if array parameter contains valueAngularJS过滤器检查数组参数是否包含值
【发布时间】:2020-06-03 10:19:57
【问题描述】:

我有一个简单的函数来根据 url 为导航设置活动状态:

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

现在我想添加要突出显示的旧 URL。我有 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】:

    您可以尝试以下与您添加的代码类似的代码。

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

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2019-08-10
      • 2015-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多