最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

Element directives - 指令遇到時(shí)激活一個(gè)匹配的元素。

Attribute - - 指令遇到時(shí)激活一個(gè)匹配的屬性。

CSS - - 指令遇到時(shí)激活匹配CSS樣式。

Comment - - 指令遇到時(shí)激活匹配的注釋。

了解自定義指令

定義自定義的HTML標(biāo)簽。

<student name="Mahesh"></student><br/>
<student name="Piyush"></student>

定義自定義指令來(lái)處理上面的自定義HTML標(biāo)簽。

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。看到結(jié)果如下:

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

相關(guān)文章

最新評(píng)論

公安县| 二手房| 肇州县| 德阳市| 扎赉特旗| 鸡东县| 卢湾区| 乡宁县| 马关县| 边坝县| 靖安县| 桃园县| 革吉县| 福州市| 额济纳旗| 贵港市| 定安县| 平度市| 岳池县| 青海省| 商南县| 天水市| 理塘县| 华阴市| 宁化县| 芜湖县| 筠连县| 黔西| 腾冲县| 高尔夫| 镇沅| 泗阳县| 永嘉县| 苍南县| 芜湖县| 东至县| 淮南市| 方正县| 桓台县| 宜城市| 古交市|