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

詳解AngularJS中自定義指令的使用

 更新時(shí)間:2015年06月17日 11:36:27   投稿:goldensun  
這篇文章主要介紹了詳解AngularJS中自定義指令的使用,包括結(jié)合自定義HTML標(biāo)簽的使用,需要的朋友可以參考下

 自定義指令中使用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??吹浇Y(jié)果如下:

2015617113318563.jpg (560×240)

相關(guān)文章

  • AngularJS基礎(chǔ) ng-class-odd 指令示例

    AngularJS基礎(chǔ) ng-class-odd 指令示例

    本文主要介紹AngularJS ng-class-odd 指令,這里對(duì)ng-class-odd基礎(chǔ)知識(shí)做了詳細(xì)整理,并有示例代碼和效果圖,學(xué)習(xí)AngularJS的同學(xué)可以參考下
    2016-08-08
  • Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能

    Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能

    本文通過(guò)實(shí)例代碼給大家介紹了Angularjs實(shí)現(xiàn)上傳圖片預(yù)覽功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • 詳解Angularjs filter過(guò)濾器

    詳解Angularjs filter過(guò)濾器

    這篇文章主要介紹了angularjs filter過(guò)濾器的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • AngularJs中route的使用方法和配置

    AngularJs中route的使用方法和配置

    angular是Google開發(fā)的一個(gè)單頁(yè)面應(yīng)用框架,是現(xiàn)在比較主流的單頁(yè)面應(yīng)用框架之一,下面通過(guò)本文給大家介紹AngularJs中route的使用方法和配置,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • angular安裝import?echarts?from‘echarts‘標(biāo)紅報(bào)錯(cuò)解決

    angular安裝import?echarts?from‘echarts‘標(biāo)紅報(bào)錯(cuò)解決

    這篇文章主要介紹了angular安裝import?echarts?from‘echarts‘標(biāo)紅報(bào)錯(cuò)解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • AngularJS入門教程之靜態(tài)模板詳解

    AngularJS入門教程之靜態(tài)模板詳解

    本文主要介紹AngularJS 靜態(tài)模板,這里整理了相關(guān)的基礎(chǔ)資料,會(huì)幫助大家學(xué)習(xí)基礎(chǔ)的AngularJS的基礎(chǔ)知識(shí),有需要的小伙伴可以參考下
    2016-08-08
  • AngularJS基礎(chǔ)知識(shí)筆記之表格

    AngularJS基礎(chǔ)知識(shí)筆記之表格

    這篇文章主要介紹了AngularJS基礎(chǔ)知識(shí)筆記之表格的相關(guān)資料,需要的朋友可以參考下
    2015-05-05
  • 詳解AngularJs中$resource和restfu服務(wù)端數(shù)據(jù)交互

    詳解AngularJs中$resource和restfu服務(wù)端數(shù)據(jù)交互

    之前小編和大家分享過(guò)使用$http同服務(wù)器進(jìn)行通信,但是功能上比較簡(jiǎn)單,angularjs還提供了另外一個(gè)可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進(jìn)行數(shù)據(jù)交互。下面來(lái)一起看看吧。
    2016-09-09
  • angular實(shí)現(xiàn)表單驗(yàn)證及提交功能

    angular實(shí)現(xiàn)表單驗(yàn)證及提交功能

    這篇文章主要為大家詳細(xì)介紹了angular實(shí)現(xiàn)表單驗(yàn)證及提交功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Angular 4依賴注入學(xué)習(xí)教程之組件服務(wù)注入(二)

    Angular 4依賴注入學(xué)習(xí)教程之組件服務(wù)注入(二)

    大家都知道依賴注入式AngularJS的重要特性之一,之前我們已經(jīng)介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之組件服務(wù)注入的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-06-06

最新評(píng)論