AngularJS – ng-switch Directive

AngularJSWeb DevelopmentFront End Technology

The ngSwitch directive in AngularJS conditionally swaps the DOM structure on the template based upon the scope expression. This directive can be used for showing or hiding elements based upon the switch cases.

The HTML elements will be displayed only if the expression inside the ngSwitch directive evaluates to True, else it will remain hidden. This directive is supported by all the HTML elements.

Syntax

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

Example − ngSwitch Directive

Create a file "ngSwitch.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>ngSwitch Directive</title>

      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">             </script>
   </head>

   <body ng-app style="text-align: center;">
      <h1 style="color: green;">
         Welcome to Tutorials Point
      </h1>
      <h2>
         AngularJS | ngSwitch Directive
      </h2>

      <div>
         <form>
            <label>
               <input type="radio" value="search" ng-model="switch.Data" />
               FrontEnd Frameworks
            </label>
            <label>
               <input type="radio" value="sort" ng-model="switch.Data" />
               Backend Technologies
            </label>
         </form>

         <div ng-switch="switch.Data" id="wrapper">
            <div ng-switch-when="search">
               <h2>FrontEnd Frameworks</h2>
               <ul>
                  <li>Angular JS</li>
                  <li>React JS</li>
                  <li>Vue JS</li>
               </ul>
            </div>
            <div ng-switch-when="sort">
               <h2>Backend Technologies</h2>
               <ul>
                  <li>C</li>
                  <li>C++</li>
                  <li>Java</li>
               </ul>
</div>
</div>
</div>
</body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Example 2

Create a file "ngSwitch.html" in your Angular project directory and copy-paste the following code snippet.

<!DOCTYPE html>
<html>
   <head>
      <title>ngSwitch Directive</title>

      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
      </script>
   </head>

   <body ng-app style="text-align: center;">

   <h1 style="color:green">
      Welcome to Tutorials Point
   </h1>
      <h2>
         AngularJS | ngSwitch Directive
   </h2>

   <div>
      <div class="col-md-3">
         Type value(true/false):
         <input ng-model="value" type="value" />
      </div><br>

      <div ng-switch="value" class="col-md-3">
         <div ng-switch-when="true" class="btn btn-danger">
            You entered <b>{{value}}</b>
         </div>

         <div ng-switch-when="false" class="btn btn-primary">
            You entered <b>{{value}}</b>
         </div>

         <div ng-switch-default class="well">
            This is the default section.
         </div>
      </div>
   </div>

</body>
</html>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

raja
Updated on 08-Oct-2021 11:48:00

Advertisements