0

我需要写一个ng-switch有4个条件的角度。

SO上的其他一些答案建议使用ng-if而不是ng-switch.

所以我尝试了ng-if,但我真的不认为这是最好的方法,我会解释为什么:

基本上我需要显示 3 个不同的图标,这取决于我的范围内的一些变量。

我的模板中的代码如下:

<div class="buttons">
   <div ng-if="test.mMode==4 && test.kit==4 && test.react.on==true && test.act==1">
      <i class="icon ion-man"></i>
   </div>

   <div ng-if="test.mMode==4 && test.kit==4 && test.react.on==false && test.act==1">
      <i class="icon ion-man"></i>
   </div>

   <div ng-if="zone.iActivity==1">
      <i class="icon ion-man"></i>
   </div>
</div>

现在,对我来说,这看起来真的很乱。而且,它没有按预期工作,因为它遍历所有打印所有真实的 if 语句。

是否可以做类似以下的事情?

<div class="buttons" ng-switch="test.act && test.Mode && test.kit && test.react.on">
   <div ng-switch-when="1 && 4 && 4 && true">
      <i class="icon ion-man"></i>
   </div>

   <div ng-switch-when="1 && 4 && 4 && false">
      <i class="icon ion-man"></i>
   </div>

   <div ng-switch-when="1"> //I need only the first condition to be true, I don't need the others.
      <i class="icon ion-man"></i>
   </div>
</div>

或者,您对如何正确处理此类模板还有其他建议吗?

希望问题足够清楚。

谢谢你的帮助

4

1 回答 1

1

有点脏的解决方案是使用数组和 toString() 方法。

<ng-switch on="[Ex1.x, Ex1.y].toString()">
    <div ng-switch-when="0,true">
      0,true -- jaj
    </div>
    <div ng-switch-when="1,true">
      1,true -- jaj
    </div>
    <div ng-switch-when="2,false">
      2,false -- jaj
    </div>
    <div ng-switch-default>
     default... booo
    </div>
  </ng-switch>

小提琴:https ://jsfiddle.net/r88dhr0w/

于 2016-05-19T15:24:12.170 回答