Is it possible to have an OR in ng-switch-when?
<div ng-repeat="w in windows" ng-show="visibleWindowId == w.id" ng-switch="w.type">
<div ng-switch-when="val1 **OR** val2">
sup
</div>
</div>
If not, how could the above be accomplished?
Thanks :)
You could use
ng-class
so that you can useor
operator in your expression. Also, angular-ui hasif
directive.This is now possible using
ng-switch-when-separator
which was added to Angular documentation in 1.5.10:This will work too
I created a simple directive in place of
ngSwitchWhen
that allows you to specify multiple cases for a single tag. It also allows you to specify dynamic values instead of purely static values.One caveat, it only evaluates the expression once upon compile time, so you must return the correct value immediately. For my purposes this was fine as I was wanting to use constants defined elsewhere in the application. It could probably be modified to dynamically re-evaluate the expressions but that would require more testing with
ngSwitch
.I am use angular 1.3.15 but I ran a quick test with angular 1.4.7 and it worked fine there as well.
Plunker Demo
The Code
Usage
Controller Templatengswitch
only allows you to compare a single condition.I you are looking to test multiple conditions you can use
ng-if
available with version 1.1.5Reference
It is important to note that using
ng-if
andng-switch
remove the element from the DOM structure, opposed to show and hide.This is important when you traverse the DOM to find elements.