angularjs: multiple values in a ng-switch-when
I have the following ngSwitch:
<p ng-switch="status">
<span ng-switch-when="wrong|incorrect">
Wrong
</span>
<span ng-switch-default>
Correct
</span>
</p>
As you can see, I have the text Wrong
for two options wrong
and correct
. I have tried (as you can see) to use the pipe |
, but that doesn't work. Any suggestions ?
For angular >=v1.5.10,
You can do it by adding ng-switch-when-separator="|"
to ng-switch-when
node.
see example in documentation.
<span ng-switch-when="wrong|incorrect" ng-switch-when-separator="|">
see discussion here https://github.com/angular/angular.js/issues/3410 Note, based on my experience it doesn't work with numbers...yet?
This is almost same with using a ng-if, but the advantage of this is that you can use ng-switch-when="true" or default or false multiple times within main ng-switch.
<p ng-switch on="(status == 'wrong') || (status == 'incorrect')">
<span ng-switch-when="true">
Wrong
</span>
<span ng-switch-default>
Correct
</span>
</p>
Live : http://jsfiddle.net/8yf15t2d/
You can't have multiple conditions with a single ng-switch-when
.
One alternative is to use an ng-if
, but in the case of error handling, I prefer to populate an error variable on the scope in the controller, and use ng-show=error
.