The ngSwitch
directive is used to conditionally swap DOM structure on your template based on a scope expression.
Elements within ngSwitch
but without ngSwitchWhen
or ngSwitchDefault
directives will be preserved at the location
as specified in the template.
The directive itself works similar to ngInclude, however, instead of downloading template code (or loading it
from the template cache), ngSwitch
simply chooses one of the nested elements and makes it visible based on which element
matches the value obtained from the evaluated expression. In other words, you define a container element
(where you place the directive), place an expression on the on="..."
attribute
(or the ng-switch="..."
attribute), define any inner elements inside of the directive and place
a when attribute per element. The when attribute is used to inform ngSwitch which element to display when the on
expression is evaluated. If a matching expression is not found via a when attribute then an element with the default
attribute is displayed.
ng-switch-when="someVal"
will match against the string "someVal"
not against the
value of the expression $scope.someVal
.
<ANY ng-switch="expression">
<ANY ng-switch-when="matchValue1">...</ANY>
<ANY ng-switch-when="matchValue2">...</ANY>
<ANY ng-switch-default>...</ANY>
</ANY>
Animation | Occurs |
---|---|
enter | after the ngSwitch contents change and the matched child element is placed inside the container |
leave | after the ngSwitch contents change and just before the former contents are removed from the DOM |
Param | Type | Details |
---|---|---|
ngSwitch | on | * |
expression to match against
|
<div ng-controller="ExampleController">
<select ng-model="selection" ng-options="item for item in items">
</select>
<code>selection={{selection}}</code>
<hr/>
<div class="animate-switch-container"
ng-switch on="selection">
<div class="animate-switch" ng-switch-when="settings">Settings Div</div>
<div class="animate-switch" ng-switch-when="home">Home Span</div>
<div class="animate-switch" ng-switch-default>default</div>
</div>
</div>