【问题标题】:Angular2 - How to use an OR statement?Angular2 - 如何使用 OR 语句?
【发布时间】:2016-05-22 19:03:44
【问题描述】:

所以我正在使用活动路线来操作菜单:

我的菜单如下所示:

<li [class.router-link-active]="currentPath == '/link1'">
     <a [routerLink]="['link1']"><span>Link 1 option</span></a>
     <ul>
        <li><a [routerLink]="['link1']">Link 1 option</a></li>
        <li><a [routerLink]="['link2']">Link 2 option</a></li>
        <li><a [routerLink]="['link3']">Link 3 option</a></li>
    </ul>
</li>

主 LI 元素控制下拉菜单样式,因此我需要一个 OR 语句来确保在单击任何菜单集合项时正确设置下拉菜单。

[class.router-link-active]="currentPath == '/link1' OR currentPath == '/link2' OR currentPath == '/link3'"

Angular2 中如何使用 OR 语句?

【问题讨论】:

    标签: javascript angular


    【解决方案1】:

    你可以像这样使用数组的 indexOf 方法:

    ['link1','link2','link3'].indexOf(currentPath) > -1
    

    【讨论】:

      【解决方案2】:

      “或”的 JavaScript 运算符是 ||

      currentPath == '/link1' || currentPath == '/link2' || currentPath == '/link3'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        相关资源
        最近更新 更多