Tutorials References Exercises Bootcamps Videos Menu
Sign Up Create Website Get Certified Upgrade

AngularJS ng-switch Directive


Show a section of HTML, only if it matches a certain value:

<div ng-switch="myVar">
  <div ng-switch-when="dogs">
    <p>Welcome to a world of dogs.</p>
  <div ng-switch-when="tuts">
    <p>Learn from examples.</p>
  <div ng-switch-when="cars">
    <p>Read about cars.</p>
  <div ng-switch-default>
    <p>Select topic from the dropdown, to switch the content of this DIV.</p>
Try it Yourself »

Definition and Usage

The ng-switch directive lets you hide/show HTML elements depending on an expression.

Child elements with the ng-switch-when directive will be displayed if it gets a match, otherwise the element, and its children will be removed.

You can also define a default section, by using the ng-switch-default directive, to show a section if none of the other sections get a match.


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

Supported by all HTML elements.

Parameter Values

Value Description
expression An expression that will remove elements with no match, and display elements with a match.