ng-switchDyrektywa AngularJS


Przykład

Pokaż sekcję HTML tylko wtedy, gdy pasuje do określonej wartości:

<div ng-switch="myVar">
  <div ng-switch-when="dogs">
    <h1>Dogs</h1>
    <p>Welcome to a world of dogs.</p>
  </div>
  <div ng-switch-when="tuts">
    <h1>Tutorials</h1>
    <p>Learn from examples.</p>
  </div>
  <div ng-switch-when="cars">
    <h1>Cars</h1>
    <p>Read about cars.</p>
  </div>
  <div ng-switch-default>
    <h1>Switch</h1>
    <p>Select topic from the dropdown, to switch the content of this DIV.</p>
  </div>
</div>

Definicja i użycie

Dyrektywa ng-switchpozwala ukryć/pokazać elementy HTML w zależności od wyrażenia.

Elementy potomne z ng-switch-whendyrektywą zostaną wyświetlone, jeśli otrzyma dopasowanie, w przeciwnym razie element i jego dzieci zostaną usunięte.

Możesz również zdefiniować sekcję domyślną, używając ng-switch-default dyrektywy, aby pokazać sekcję, jeśli żadna z pozostałych sekcji nie zostanie dopasowana.


Składnia

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

Obsługiwane przez wszystkie elementy HTML.


Wartości parametrów

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