ล่าสุดการพัฒนาเว็บบทเรียน
 

AngularJS NG-Directive สวิทช์


ตัวอย่าง

แสดงส่วนของ 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 ทั้งหมด


ค่าพารามิเตอร์

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