在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ AngularJS自定義指令
AngularJS國際化
AngularJS表達式
AngularJS控制器
AngularJS MVC體系結(jié)構(gòu)
AngularJS表單
AngularJS服務(wù)
AngularJS作用域
AngularJS快速入門
AngularJS包括
AngularJS第一個應(yīng)用程序
AngularJS HTML DOM
AngularJS過濾器
AngularJS模塊
AngularJS Ajax
AngularJS自定義指令
AngularJS教程
AngularJS依賴注入
AngularJS表格
AngularJS指令
AngularJS環(huán)境設(shè)置
AngularJS視圖

AngularJS自定義指令

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

  • Element directives - 指令遇到時激活一個匹配的元素。

  • Attribute - - 指令遇到時激活一個匹配的屬性。

  • CSS - - 指令遇到時激活匹配CSS樣式。

  • Comment - - 指令遇到時激活匹配的注釋。

了解自定義指令

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

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

定義自定義指令來處理上面的自定義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 上一篇:AngularJS包括下一篇:AngularJS依賴注入