AngularJS Basic ng switch Simple Example

  • 2021-07-07 06:24:25
  • OfStack

AngularJS ng-switch Directive

Displays the corresponding part according to the selected value:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="">
 My favorite website 
<select ng-model="myVar">
 <option value="runoob">www.runoob.com
 <option value="google">www.google.com
 <option value="taobao">www.taobao.com
</select>

<hr>
<div ng-switch="myVar">
 <div ng-switch-when="runoob">
   <h1> Rookie tutorial </h1>
   <p> Welcome to the rookie tutorial </p>
 </div>
 <div ng-switch-when="google">
   <h1>Google</h1>
   <p> Welcome to visit Google</p>
 </div>
 <div ng-switch-when="taobao">
   <h1> Taobao </h1>
   <p> Welcome to Taobao </p>
 </div>
 <div ng-switch-default>
   <h1> Switch </h1>
   <p> Select different options to display the corresponding values. </p>
 </div>
</div>
<hr>

<p> ng-switch  Directive shows or hides the corresponding part based on the current value. </p>

</body>
</html>

Definition and usage

The ng-switch instruction shows or hides the corresponding part based on the expression.

The corresponding child elements use the ng-switch-when instruction. If the match is selected, select display, and other matching elements are removed.

You can set the default options by using the ng-switch-default directive, and if there is no match, the default options will be displayed.

Grammar


<element ng-switch="expression">
 <element ng-switch-when="value"></element>
 <element ng-switch-when="value"></element>
 <element ng-switch-when="value"></element>
 <element ng-switch-default></element>
</element>

< form > Element supports this attribute.

Parameter value

描述
expression 表达式会让匹配项显示,不匹配项移除。

The above is a detailed introduction to AngularJS ng-switch instruction, and continue to supplement in the follow-up. Thank you for your support to this site!


Related articles: