【问题标题】:ng-switch not working as expectedng-switch 没有按预期工作
【发布时间】:2016-04-25 08:31:30
【问题描述】:

我不想拥有两个单独的ng-if,而是想将它们组合起来使用ng-switch。但是,img() 函数没有按预期切换。我应该怎么做?

转动这个:

<img ng-src="{{img(myColor)}}" ng-if="myColor">

<img ng-src="{{img(myColor2)}}" ng-if="myColor2">

进入这个:

<div ng-switch on = "myColor2">
    <div ng-switch-when = "myColor2">
        <img ng-src="{{img(myColor2)}}">
    </div>
    <div ng-switch-default = "myColor">
        <img ng-src="{{img(myColor)}}">
    </div>
</div>

【问题讨论】:

  • 以防万一,它是ng-switch-default,而不是ng-switch default
  • @iggymoran 感谢您的收获。但这并没有解决它

标签: angularjs user-interface angular-ng-if ng-switch


【解决方案1】:

这里是 ng-switch 的示例

http://jsfiddle.net/fbg3bLac/5/

你可能理解错了。

ng-switch on = "myColor2" -- means you want to evaluate the value of the scope variable myColor2.

ng-switch-when = "myColor2" -- states that if the VALUE of myColor2 is equal to 'myColor2' then do this.

ng-switch-default -- should have no = value.

请参考http://www.c-sharpcorner.com/UploadFile/75a48f/switch-condition-in-angularjs/

【讨论】:

  • 谢谢,有没有办法完成我想做的事情?我明白你在说什么,但我有两个不同的 ng 模型
  • 您想要切换范围变量颜色的示例,然后您的切换将是 ng-switch-when="{{myColor1}}" - 尚未尝试,但它应该让 Angular 知道myColor1 是要评估的变量,而不是值本身。
猜你喜欢
  • 2017-03-23
  • 2014-10-14
  • 2017-08-20
  • 2018-04-24
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2015-11-12
相关资源
最近更新 更多