AngularJS - HTML DOM

คำสั่งต่อไปนี้ใช้เพื่อผูกข้อมูลแอปพลิเคชันกับคุณลักษณะขององค์ประกอบ HTML DOM -

ซีเนียร์ ชื่อและคำอธิบาย
1

ng-disabled

ปิดใช้งานการควบคุมที่กำหนด

2

ng-show

แสดงการควบคุมที่กำหนด

3

ng-hide

ซ่อนการควบคุมที่กำหนด

4

ng-click

แทนเหตุการณ์คลิก AngularJS

ng-disabled Directive

เพิ่มแอตทริบิวต์ ng-disabled ลงในปุ่ม HTML และส่งต่อแบบจำลอง ผูกโมเดลเข้ากับช่องทำเครื่องหมายและดูรูปแบบ

<input type = "checkbox" ng-model = "enableDisableButton">Disable Button
<button ng-disabled = "enableDisableButton">Click Me!</button>

คำสั่ง ng-show

เพิ่มแอตทริบิวต์ ng-show ลงในปุ่ม HTML และส่งแบบจำลอง ผูกโมเดลเข้ากับช่องทำเครื่องหมายและดูรูปแบบ

<input type = "checkbox" ng-model = "showHide1">Show Button
<button ng-show = "showHide1">Click Me!</button>

คำสั่ง ng-hide

เพิ่มแอตทริบิวต์ ng-hide ลงในปุ่ม HTML และส่งต่อแบบจำลอง ผูกโมเดลเข้ากับช่องทำเครื่องหมายและดูรูปแบบ

<input type = "checkbox" ng-model = "showHide2">Hide Button
<button ng-hide = "showHide2">Click Me!</button>

ng-click Directive

เพิ่มแอตทริบิวต์ ng-click ลงในปุ่ม HTML และอัปเดตโมเดล ผูกโมเดลกับ HTML และดูรูปแบบ

<p>Total click: {{ clickCounter }}</p>
<button ng-click = "clickCounter = clickCounter + 1">Click Me!</button>

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดงการใช้คำสั่งที่กล่าวถึงข้างต้นทั้งหมด

testAngularJS.htm

<html>
   <head>
      <title>AngularJS HTML DOM</title>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      
      <div ng-app = "">
         <table border = "0">
            <tr>
               <td><input type = "checkbox" ng-model = "enableDisableButton">Disable Button</td>
               <td><button ng-disabled = "enableDisableButton">Click Me!</button></td>
            </tr>
            <tr>
               <td><input type = "checkbox" ng-model = "showHide1">Show Button</td>
               <td><button ng-show = "showHide1">Click Me!</button></td>
            </tr>
            <tr>
               <td><input type = "checkbox" ng-model = "showHide2">Hide Button</td>
               <td><button ng-hide = "showHide2">Click Me!</button></td>
            </tr>
            <tr>
               <td><p>Total click: {{ clickCounter }}</p></td>
               <td><button ng-click = "clickCounter = clickCounter + 1">Click Me!</button></td>
            </tr>
         </table>
      </div>
      
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      </script>
      
   </body>
</html>

เอาต์พุต

เปิดไฟล์testAngularJS.htmในเว็บเบราว์เซอร์และดูผลลัพธ์