AngularJS 指令
AngularJS 通過被稱為 指令 的新屬性來擴展 HTML。
AngularJS 通過內置的指令來為應用添加功能。
AngularJS 允許你自定義指令。
AngularJS 指令
AngularJS 指令是擴展的 HTML 屬性,帶有前綴 ng-。
ng-app 指令初始化一個 AngularJS 應用程序。
ng-init 指令初始化應用程序數(shù)據(jù)。
ng-model 指令把元素值(比如輸入域的值)綁定到應用程序。
完整的指令內容可以參閱 AngularJS 參考手冊。
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="" ng-init="firstName='John'"> <p>在輸入框中嘗試輸入:</p> <p>姓名: <input type="text" ng-model="firstName"></p> <p>你輸入的為: {{ firstName }}</p> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
ng-app 指令告訴 AngularJS,<div> 元素是 AngularJS 應用程序 的"所有者"。
![]() | 一個網(wǎng)頁可以包含多個運行在不同元素中的 AngularJS 應用程序。 |
---|
數(shù)據(jù)綁定
上面實例中的 {{ firstName }} 表達式是一個 AngularJS 數(shù)據(jù)綁定表達式。
AngularJS 中的數(shù)據(jù)綁定,同步了 AngularJS 表達式與 AngularJS 數(shù)據(jù)。
{{ firstName }} 是通過 ng-model="firstName" 進行同步。
在下一個實例中,兩個文本域是通過兩個 ng-model 指令同步的:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div data-ng-app="" data-ng-init="quantity=1;price=5"> <h2>價格計算器</h2> 數(shù)量: <input type="number" ng-model="quantity"> 價格: <input type="number" ng-model="price"> <p><b>總價:</b> {{quantity * price}}</p> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
![]() | 使用 ng-init 不是很常見。您將在控制器一章中學習到一個更好的初始化數(shù)據(jù)的方式。 |
---|
重復 HTML 元素
ng-repeat 指令會重復一個 HTML 元素:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div data-ng-app="" data-ng-init="names=['Jani','Hege','Kai']"> <p>使用 ng-repeat 來循環(huán)數(shù)組</p> <ul> <li data-ng-repeat="x in names"> {{ x }} </li> </ul> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
ng-repeat 指令用在一個對象數(shù)組上:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="" ng-init="names=[ {name:'Jani',country:'Norway'}, {name:'Hege',country:'Sweden'}, {name:'Kai',country:'Denmark'}]"> <p>循環(huán)對象:</p> <ul> <li ng-repeat="x in names"> {{ x.name + ', ' + x.country }}</li> </ul> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
![]() | AngularJS 完美支持數(shù)據(jù)庫的 CRUD(增加Create、讀取Read、更新Update、刪除Delete)應用程序。 把實例中的對象想象成數(shù)據(jù)庫中的記錄。 |
---|
ng-app 指令
ng-app 指令定義了 AngularJS 應用程序的 根元素。
ng-app 指令在網(wǎng)頁加載完畢時會自動引導(自動初始化)應用程序。
稍后您將學習到 ng-app 如何通過一個值(比如 ng-app="myModule")連接到代碼模塊。
ng-init 指令
ng-init 指令為 AngularJS 應用程序定義了 初始值。
通常情況下,不使用 ng-init。您將使用一個控制器或模塊來代替它。
稍后您將學習更多有關控制器和模塊的知識。
ng-model 指令
ng-model 指令 綁定 HTML 元素 到應用程序數(shù)據(jù)。
ng-model 指令也可以:
為應用程序數(shù)據(jù)提供類型驗證(number、email、required)。
為應用程序數(shù)據(jù)提供狀態(tài)(invalid、dirty、touched、error)。
為 HTML 元素提供 CSS 類。
綁定 HTML 元素到 HTML 表單。
ng-repeat 指令
ng-repeat 指令對于集合中(數(shù)組中)的每個項會 克隆一次 HTML 元素。
創(chuàng)建自定義的指令
除了 AngularJS 內置的指令外,我們還可以創(chuàng)建自定義指令。
你可以使用 .directive 函數(shù)來添加自定義的指令。
要調用自定義指令,HTML 元素上需要添加自定義指令名。
使用駝峰法來命名一個指令, phpDirective, 但在使用它時需要以 - 分割, php-directive:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <php-directive></php-directive> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { template : "<h1>自定義指令!</h1>" }; }); </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
你可以通過以下方式來調用指令:
元素名
屬性
類名
注釋
以下實例方式也能輸出同樣結果:
元素名
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <php-directive></php-directive> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { template : "<h1>自定義指令!</h1>" }; }); </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
屬性
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <div php-directive></div> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { template : "<h1>自定義指令!</h1>" }; }); </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
類名
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <div class="php-directive"></div> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { restrict : "C", template : "<h1>自定義指令!</h1>" }; }); </script> <p><strong>注意:</strong> 你必須設置 <b>restrict</b> 的值為 "C" 才能通過類名來調用指令。</p> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
注釋
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <!-- directive: php-directive --> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { restrict : "M", replace : true, template : "<h1>自定義指令!</h1>" }; }); </script> <p><strong>注意:</strong> 我們需要在該實例添加 <strong>replace</strong> 屬性, 否則評論是不可見的。</p> <p><strong>注意:</strong> 你必須設置 <b>restrict</b> 的值為 "M" 才能通過注釋來調用指令。</p> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
限制使用
你可以限制你的指令只能通過特定的方式來調用。
實例
通過添加 restrict 屬性,并設置只值為 "A"
,
來設置指令只能通過屬性的方式來調用:
實例
<!DOCTYPE html> <html><head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body ng-app="myApp"> <php-directive></php-directive> <div php-directive></div> <script> var app = angular.module("myApp", []); app.directive("phpDirective", function() { return { restrict : "A", template : "<h1>自定義指令!</h1>" }; }); </script> <p><strong>注意:</strong> 通過設置 <strong>restrict</strong> 屬性值為 "A" 來設置指令只能通過 HTML 元素的屬性來調用。</p> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
restrict 值可以是以下幾種:
E
作為元素名使用A
作為屬性使用C
作為類名使用M
作為注釋使用
restrict 默認值為 EA
, 即可以通過元素名和屬性名來調用指令。