欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS 自定義指令詳解及示例代碼

 更新時間:2016年08月17日 15:38:25   作者:zhangjie_0303  
本文主要介紹AngularJS 自定義指令,這里整理了基礎(chǔ)資料及詳細的示例代碼及實現(xiàn)效果圖,有需要的小伙伴可以參考下

自定義指令中使用AngularJS擴展HTML的功能。自定義指令使用的“指令”的功能定義。自定義指令只是替換了它被激活的元素。引導過程中AngularJS應用程序找到了匹配的元素,并做好使用自定義指令compile()方法一次活動再處理使用基于指令的范圍自定義指令link()方法的元素。 AngularJS提供支持,以下列元素的類型來創(chuàng)建自定義指令。

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 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="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>

結(jié)果

在Web瀏覽器中打開textAngularJS.html??吹浇Y(jié)果如下:

以上就是對AngularJS的自定義指令資料整理,后續(xù)繼續(xù)補充,謝謝大家對本站的支持!

相關(guān)文章

  • AngularJS入門教程之Hello World!

    AngularJS入門教程之Hello World!

    這篇文章主要介紹了AngularJS入門教程之Hello World!,本文用經(jīng)典的應用程序“Hello World!”來講解AngularJS,要的朋友可以參考下
    2014-12-12
  • AngularJS控制器繼承自另一控制器

    AngularJS控制器繼承自另一控制器

    本文給大家介紹AngularJS控制器繼承自另一控制器的相關(guān)內(nèi)容,小編認為介紹的非常不錯,具有參考借鑒價值,感興趣的朋友參考下吧
    2016-05-05
  • Angular利用HTTP POST下載流文件的步驟記錄

    Angular利用HTTP POST下載流文件的步驟記錄

    這篇文章主要給大家介紹了關(guān)于Angular利用HTTP POST下載流文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Angular具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-07-07
  • AngularJS動態(tài)綁定ng-options的ng-model實例代碼

    AngularJS動態(tài)綁定ng-options的ng-model實例代碼

    本篇文章主要介紹了AngularJS動態(tài)綁定ng-options的ng-model實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • angular4響應式表單與校驗實現(xiàn)demo

    angular4響應式表單與校驗實現(xiàn)demo

    這篇文章主要介紹了angular4響應式表單與校驗實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • AngularJS實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實例

    AngularJS實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實例

    這篇文章給大家分享了AngularJS循環(huán)實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等基礎(chǔ)功能,對大家學習AngularJS具有一定的參考借鑒價值,有需要的朋友可以看看。
    2016-09-09
  • Angular value與ngValue區(qū)別詳解

    Angular value與ngValue區(qū)別詳解

    這篇文章主要介紹了Angular value與ngValue區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • 說說AngularJS中的$parse和$eval的用法

    說說AngularJS中的$parse和$eval的用法

    本篇文章主要介紹了說說AngularJS中的$parse和$eval的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Angular 4.x中表單Reactive Forms詳解

    Angular 4.x中表單Reactive Forms詳解

    這篇文章主要介紹了Angular 4.x中表單Reactive Forms的相關(guān)資料,文中通過示例代碼介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • angular和BootStrap3實現(xiàn)購物車功能

    angular和BootStrap3實現(xiàn)購物車功能

    這篇文章主要為大家詳細介紹了angular和BootStrap3實現(xiàn)購物車功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評論