ngSwitch (directive)

Improve this Doc View Source ngSwitch

  1. directive in module ng

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.

Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal.

Directive Info

  • This directive creates new scope.
  • This directive executes at priority level 1200.

Usage

<ANY ng-switch="expression">
  <ANY ng-switch-when="matchValue1">...</ANY>
  <ANY ng-switch-when="matchValue2">...</ANY>
  <ANY ng-switch-default>...</ANY>
</ANY>

Animations

enter - happens after the ngSwitch contents change and the matched child element is placed inside the container leave - happens just after the ngSwitch contents change and just before the former contents are removed from the DOM

Click here to learn more about the steps involved in the animation.

Arguments

Param Type Details
ngSwitch | on *

expression to match against ng-switch-when. On child elements add:

  • ngSwitchWhen: the case statement to match against. If match then this case will be displayed. If the same match appears multiple times, all the elements will be displayed.
  • ngSwitchDefault: the default case when no other case match. If there are multiple default cases, all of them will be displayed when no other case match.

© 2010–2016 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.3.20/docs/api/ng/directive/ngSwitch

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部