詳解AngularJS中自訂指令的使用,詳解angularjs

來源:互聯網
上載者:User

詳解AngularJS中自訂指令的使用,詳解angularjs

 自訂指令中使用AngularJS擴充HTML的功能。自訂指令使用的“指令”的功能定義。自訂指令只是替換了它被啟用的元素。引導過程中AngularJS應用程式找到了匹配的元素,並做好使用自訂指令compile()方法一次活動再處理使用基於指令的範圍自訂指令link()方法的元素。 AngularJS提供支援,以下列元素的類型來建立自訂指令。

  •     Element directives - 指令遇到時啟用一個匹配的元素。
  •     Attribute - - 指令遇到時啟用一個匹配的屬性。
  •     CSS - - 指令遇到時啟用匹配CSS樣式。
  •     Comment - - 指令遇到時啟用匹配的注釋。

瞭解自訂指令

定義自訂的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 htmlmainApp.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="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/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>

結果

在Web瀏覽器中開啟textAngularJS.html。看到結果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.