
- AngularJS 教程
- AngularJS - 首頁
- AngularJS - 概述
- AngularJS - 環境搭建
- AngularJS - MVC 架構
- AngularJS - 第一個應用程式
- AngularJS - 指令
- AngularJS - 表示式
- AngularJS - 控制器
- AngularJS - 過濾器
- AngularJS - 表格
- AngularJS - HTML DOM
- AngularJS - 模組
- AngularJS - 表單
- AngularJS - 包含
- AngularJS - AJAX
- AngularJS - 檢視
- AngularJS - 作用域
- AngularJS - 服務
- AngularJS - 依賴注入
- AngularJS - 自定義指令
- AngularJS - 國際化
- AngularJS 應用
- AngularJS - ToDo 應用
- AngularJS - 記事本應用
- AngularJS - Bootstrap 應用
- AngularJS - 登入應用
- AngularJS - 上傳檔案
- AngularJS - 內聯應用
- AngularJS - 導航選單
- AngularJS - 切換選單
- AngularJS - 訂單表單
- AngularJS - 搜尋標籤
- AngularJS - 拖拽應用
- AngularJS - 購物車應用
- AngularJS - 翻譯應用
- AngularJS - 圖表應用
- AngularJS - 地圖應用
- AngularJS - 分享應用
- AngularJS - 天氣應用
- AngularJS - 計時器應用
- AngularJS - Leaflet 應用
- AngularJS - Lastfm 應用
- AngularJS 有用資源
- AngularJS - 問答
- AngularJS - 快速指南
- AngularJS - 有用資源
- AngularJS - 討論
AngularJS - 自定義指令
自定義指令用於擴充套件HTML的功能。自定義指令使用“directive”函式定義。自定義指令簡單地替換其啟用的元素。AngularJS應用程式在引導過程中查詢匹配的元素,並使用自定義指令的compile()方法進行一次性操作,然後根據指令的作用域使用自定義指令的link()方法處理元素。AngularJS支援為以下型別的元素建立自定義指令。
元素指令 - 遇到匹配的元素時啟用指令。
屬性 - 遇到匹配的屬性時啟用指令。
CSS - 遇到匹配的css樣式時啟用指令。
註釋 - 遇到匹配的註釋時啟用指令。
理解自定義指令
定義自定義html標籤。
<student name = "Mahesh"></student><br/> <student name = "Piyush"></student>
定義自定義指令來處理上述自定義html標籤。
var mainApp = angular.module("mainApp", []); //Create a directive, first parameter is the html element to be attached. //We are attaching student html tag. //This directive will be activated as soon as any student element is encountered in html mainApp.directive('student', function() { //define the directive object var directive = {}; //restrict = E, signifies that directive is Element directive directive.restrict = 'E'; //template replaces the complete element with its text. directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>"; //scope is used to distinguish each student element based on criteria. directive.scope = { student : "=name" } //compile is called during application initialization. AngularJS calls it once when html page is loaded. directive.compile = function(element, attributes) { element.css("border", "1px solid #cccccc"); //linkFunction is linked with each element with scope to get the element specific data. var linkFunction = function($scope, element, attributes) { element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>"); element.css("background-color", "#ff00ff"); } return linkFunction; } return directive; });
定義控制器來更新指令的作用域。這裡我們使用name屬性的值作為作用域的子級。
mainApp.controller('StudentController', function($scope) { $scope.Mahesh = {}; $scope.Mahesh.name = "Mahesh Parashar"; $scope.Mahesh.rollno = 1; $scope.Piyush = {}; $scope.Piyush.name = "Piyush Parashar"; $scope.Piyush.rollno = 2; });
示例
<html> <head> <title>Angular JS Custom Directives</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = "mainApp" ng-controller = "StudentController"> <student name = "Mahesh"></student><br/> <student name = "Piyush"></student> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> <script> var mainApp = angular.module("mainApp", []); mainApp.directive('student', function() { var directive = {}; directive.restrict = 'E'; directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>"; directive.scope = { student : "=name" } directive.compile = function(element, attributes) { element.css("border", "1px solid #cccccc"); var linkFunction = function($scope, element, attributes) { element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>"); element.css("background-color", "#ff00ff"); } return linkFunction; } return directive; }); mainApp.controller('StudentController', function($scope) { $scope.Mahesh = {}; $scope.Mahesh.name = "Mahesh Parashar"; $scope.Mahesh.rollno = 1; $scope.Piyush = {}; $scope.Piyush.name = "Piyush Parashar"; $scope.Piyush.rollno = 2; }); </script> </body> </html>
輸出
在網頁瀏覽器中開啟textAngularJS.htm。檢視結果。
廣告