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

詳解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)型來(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瀏覽器中打開(kāi)textAngularJS.html??吹浇Y(jié)果如下:

2015617113318563.jpg (560×240)

相關(guān)文章

  • 關(guān)于angularJs指令的Scope(作用域)介紹

    關(guān)于angularJs指令的Scope(作用域)介紹

    下面小編就為大家?guī)?lái)一篇angularJs指令的Scope(作用域)介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • 詳解AngularJS2 Http服務(wù)

    詳解AngularJS2 Http服務(wù)

    本篇文章主要介紹了詳解AngularJS2 Http服務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • AngularJS下對(duì)數(shù)組的對(duì)比分析

    AngularJS下對(duì)數(shù)組的對(duì)比分析

    下面小編就為大家?guī)?lái)一篇AngularJS下對(duì)數(shù)組的對(duì)比分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • 用angular實(shí)現(xiàn)多選按鈕的全選與反選實(shí)例代碼

    用angular實(shí)現(xiàn)多選按鈕的全選與反選實(shí)例代碼

    本篇文章主要介紹了用angular實(shí)現(xiàn)多選按鈕的全選與反選實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Angular腳手架開(kāi)發(fā)的實(shí)現(xiàn)步驟

    Angular腳手架開(kāi)發(fā)的實(shí)現(xiàn)步驟

    這篇文章主要介紹了Angular腳手架開(kāi)發(fā)的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解關(guān)于Angular4 ng-zorro使用過(guò)程中遇到的問(wèn)題

    詳解關(guān)于Angular4 ng-zorro使用過(guò)程中遇到的問(wèn)題

    這篇文章主要介紹了詳解關(guān)于Angular4 ng-zorro使用過(guò)程中遇到的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • angular2 組件之間通過(guò)service互相傳遞的實(shí)例

    angular2 組件之間通過(guò)service互相傳遞的實(shí)例

    今天小編就為大家分享一篇angular2 組件之間通過(guò)service互相傳遞的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 淺談angular表單提交中ng-submit的默認(rèn)使用方法

    淺談angular表單提交中ng-submit的默認(rèn)使用方法

    今天小編就為大家分享一篇淺談angular表單提交中ng-submit的默認(rèn)使用方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Angular.js中處理頁(yè)面閃爍的方法詳解

    Angular.js中處理頁(yè)面閃爍的方法詳解

    我們?cè)趹?yīng)用的頁(yè)面或者組件需要加載數(shù)據(jù)時(shí),瀏覽器和angular渲染頁(yè)面都需要消耗一定的時(shí)間。這里的間隔可能很小,甚至讓人感覺(jué)不到區(qū)別;但也可能很長(zhǎng),這樣會(huì)導(dǎo)致讓我們的用戶看到了沒(méi)有被渲染過(guò)的頁(yè)面。本文將介紹Angular.js中處理頁(yè)面閃爍的方法。
    2017-03-03
  • Angular應(yīng)用prerender預(yù)渲染提高頁(yè)面加載速度

    Angular應(yīng)用prerender預(yù)渲染提高頁(yè)面加載速度

    這篇文章主要介紹了Angular應(yīng)用prerender預(yù)渲染提高頁(yè)面加載速度,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10

最新評(píng)論

肥西县| 恩平市| 阿勒泰市| 巴马| 武宁县| 玛曲县| 汶上县| 英德市| 汶川县| 皮山县| 临潭县| 泸溪县| 大新县| 三门峡市| 武鸣县| 潮安县| 德庆县| 慈利县| 龙川县| 长海县| 敖汉旗| 祥云县| 株洲县| 镇赉县| 湘西| 电白县| 舒兰市| 旬阳县| 衡山县| 河曲县| 涪陵区| 高淳县| 工布江达县| 筠连县| 涞源县| 丹江口市| 嘉荫县| 安多县| 太仆寺旗| 屏东市| 西充县|