AngularJS ng-switch
指令
示例
顯示一個 HTML 部分,僅當它匹配某個特定值時
<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>
自己動手試一試 »
定義和用法
ng-switch
指令允許您根據表示式隱藏/顯示 HTML 元素。
帶有 ng-switch-when
指令的子元素將在匹配時顯示,否則該元素及其子元素將被移除。
您還可以透過使用 ng-switch-default
指令來定義一個預設部分,以便在其他部分均未匹配時顯示一個部分。
語法
<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>
所有 HTML 元素均支援。
引數值
值 | 描述 |
---|---|
expression | 一個表示式,它將刪除不匹配的元素,並顯示匹配的元素。 |