DIV CSS 佈局教程網

AngularJS 指令(Directive)詳解:內建指令與自定義實作
編輯:JavaScript基礎知識     

AngularJS 通過一套被稱為「指令」的新屬性來擴展 HTML:內建指令為應用提供基礎能力,同時它也允許開發者自定義指令。指令一律帶有 ng- 前綴。

幾個最常用的內建指令:ng-app 初始化一個 AngularJS 應用;ng-init 設置初始數據;ng-model 把元素值綁定到應用數據。

基礎實例

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://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>

運行結果:姓名輸入框預填 John,右側同步顯示 John。

ng-app 告訴 AngularJS,這個 <div> 是應用的所有者。一個網頁中可以包含多個運行在不同元素上的 AngularJS 應用。

數據綁定

{{ firstName }} 是一個 AngularJS 表達式,它與數據的同步由 ng-model="firstName" 建立。下面這個例子用兩個 ng-model 實現了即時計算:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://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">
    價格計算器
    數量:<input type="number" ng-model="quantity">
    價格:<input type="number" ng-model="price">
    <p><b>總價:</b> {{ quantity * price }}</p>
  </div>
</body>
</html>

值得一提的是,實際項目中不推薦用 ng-init 初始化數據,更好的位置是控制器。

ng-repeat:重復渲染

循環數組:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://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 來循環數組</p>
    <ul>
      <li data-ng-repeat="x in names">{{ x }}</li>
    </ul>
  </div>
</body>
</html>

循環對象數組:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://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>循環對象:</p>
    <ul>
      <li ng-repeat="x in names">{{ x.name + ', ' + x.country }}</li>
    </ul>
  </div>
</body>
</html>

把示例中的對象想象成數據庫記錄,不難看出 AngularJS 非常適合支撐 CRUD(增、查、改、刪)類應用。

四個核心內建指令的職責

ng-app:定義應用的根元素,頁面加載完成後自動完成初始化;後續可以通過 ng-app="myModule" 關聯到代碼模塊。

ng-init:定義初始值,但通常應由控制器或模塊代替。

ng-model:把 HTML 元素綁定到應用數據,同時提供類型校驗(number、email、required)、狀態標記(invalid、dirty、touched、error)、對應的 CSS 類,並支持與表單集成。

ng-repeat:對集合中的每一項克隆一次 HTML 元素。

自定義指令

用 .directive 函數註冊自定義指令,命名採用駝峰式(如 runoobDirective),在 HTML 中則以連字符分隔(runoob-directive)。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
  <runoob-directive></runoob-directive>

  <script>
    var app = angular.module("myApp", []);
    app.directive("runoobDirective", function () {
      return {
        template: "自定義指令!"
      };
    });
  </script>
</body>
</html>

指令共有四種調用方式:元素名、屬性、類名、注釋。

以屬性方式調用

<div runoob-directive></div>

以類名方式調用——必須把 restrict 設為 "C":

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
  <div class="runoob-directive"></div>

  <script>
    var app = angular.module("myApp", []);
    app.directive("runoobDirective", function () {
      return {
        restrict: "C",
        template: "自定義指令!"
      };
    });
  </script>
</body>
</html>

以注釋方式調用——restrict 設為 "M",並需要加上 replace: true,否則注釋本身不可見:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
  <!-- directive: runoob-directive -->

  <script>
    var app = angular.module("myApp", []);
    app.directive("runoobDirective", function () {
      return {
        restrict: "M",
        replace: true,
        template: "自定義指令!"
      };
    });
  </script>
</body>
</html>

用 restrict 限制調用方式

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
  <runoob-directive></runoob-directive>
  <div runoob-directive></div>

  <script>
    var app = angular.module("myApp", []);
    app.directive("runoobDirective", function () {
      return {
        restrict: "A",   // 只允許以屬性方式調用
        template: "自定義指令!"
      };
    });
  </script>
</body>
</html>

restrict 的取值含義:

E:僅限元素名
A:僅限屬性
C:僅限類名
M:僅限注釋

默認值為 "EA",也就是可以通過元素名和屬性名調用。可以組合使用,例如 "EAC" 表示三種方式都允許。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved