Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: ng-switch-when with an OR

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 :)

like image 493
Pavel Avatar asked Jan 24 '13 02:01

Pavel


People also ask

Which directive allows to conditionally swaps the contents of a section based upon matched value?

The ngSwitch directive is used to conditionally swap DOM structure on your template based on a scope expression.

Can you use Ng-if and Ng show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.

How do you use ngSwitchDefault?

The element with ngSwitchDefault is displayed only if no match is found. The inner element with ngSwitchDefault can be placed anywhere inside the container element and not necessarily at the bottom. If you add more than one ngSwitchDefault directive, all of them are displayed.

Why do we use NG change?

The ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.


2 Answers

ngswitch 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.5

Reference

It is important to note that using ng-if and ng-switch remove the element from the DOM structure, opposed to show and hide.

This is important when you traverse the DOM to find elements.

like image 193
Malkus Avatar answered Sep 21 '22 10:09

Malkus


This is now possible using ng-switch-when-separator which was added to Angular documentation in 1.5.10:

<div ng-repeat="w in windows" ng-show="visibleWindowId == w.id" ng-switch="w.type">     <div ng-switch-when="val1|val2" ng-switch-when-separator="|">         sup     </div> </div> 
like image 23
searlea Avatar answered Sep 22 '22 10:09

searlea