angularjs自定義指令complie和link屬性


一,angularjs編譯的三個階段

1.將html轉換為DOM;

2.搜索匹配的directive,按照priority排序(默認優先級是0,ng-repeat為1000),並執行directive上的complie方法;

3.執行directive上的link方法,該方法主要進行scope綁定及事件綁定。

二.complie屬性是一個function,, 在其中可以更改dom元素,或者進行dom元素的事件綁定等

complie:function(tElement,tAttrs,transclude){

return ...

}可以返回一個對象或函數,直接返回一個函數的話就是postLink函數,這個函數其實就是單獨的link函數,也可以返回一個對象,包含prelink和postlink,如果compile函數定義了參數后,將無視link函數,因為complie里面返回的就是需要執行的link函數。

參數解釋:

 tElement 正在執行該指令的當前dom元素的jquery對象;  

 tAttrs   正在執行該指令的當前dom元素的屬性;

 transclude 模版替換之前的內容

三. link(scope,iElement,iAttrs,controller)

1.link函數代表的是complie返回的postLink函數;

2.preLink表示在編譯階段之后,在子元素被鏈接之前執行;

3.postLink會在所有子元素被鏈接之后執行;

  1. var i=0;  
  2. angular.module('myApp',[])  
  3.   
  4.     //定義第一個指令:customTags  
  5.     .directive('customTags',function(){  
  6.         return {  
  7.             restrict:'ECAM',  
  8.             template:'<div>{{ user.name }}</div>',  
  9.             replace:true,  
  10.             //定義了compile就不需定義link,當compile返回一個方法這個方法就是link  
  11.             //tElement 正在執行該指令的當前dom元素的jquery對象  
  12.             //tAttrs   正在執行該指令的當前dom元素的屬性  
  13.             compile:function(tElement,tAttrs,transclude){  
  14.                 //第一個指令的編譯階段...  
  15.                 console.log('customTags compile 編譯階段...');  
  16.   
  17.                 //若要改變dom元素,應在compile中做,此處在當前dom元素中添加一個<span>  
  18.                 tElement.append(angular.element('<span> {{user.count}}</span>'));  
  19.   
  20.                 return {  
  21.                     //pre:編譯階段之后執行  
  22.                     pre:function preLink(scope,iElement,iAttrs,controller){  
  23.                         console.log('customTags preLink..');  
  24.                     },  
  25.                     //post:所有子元素指令的post都執行后執行,此處設置了dom元素的點擊事件  
  26.                     post:function postLink(scope,iElement,iAttrs,controller){  
  27.   
  28.                         iElement.on('click',function(){  
  29.                             scope.$apply(function(){  
  30.                                 scope.user.name=' click after';  
  31.                                 scope.user.count= ++i;  
  32.                             });  
  33.                         });  
  34.                           
  35.                         console.log('customTags post end.');  
  36.                         console.log('');  
  37.                     }  
  38.                 };  
  39.   
  40.                 //compile也可直接返回一個方法,這就是 postLink,也就是上面的post  
  41.                 /*return function (){ 
  42.                     console.log('compile return this function') 
  43.                 }*/  
  44.             },  
  45.             //進行scope綁定及事件綁定  
  46.             link:function(scope,iElement,iAttrs,bookListController){  
  47.                 //link不會再執行了,因為這里定義的就是postLink  
  48.             }  
  49.         }  
  50.     })  
  51.   
  52.     //定義第二個指令:customTags2  
  53.     .directive('customTags2',function(){  
  54.         return {  
  55.             restrict:'ECAM',  
  56.             replace:true,  
  57.             compile:function(tElement,tAttrs,transclude){  
  58.                 console.log('customTags2 compile 編譯階段...');  
  59.                 return {  
  60.                     pre:function preLink(){  
  61.                         console.log('customTags2 preLink..');  
  62.                     },  
  63.                     post:function postLink(){  
  64.                         console.log('customTags2 post end.');  
  65.                     }  
  66.                 };  
  67.             }  
  68.         }  
  69.     })  
  70.   
  71.     .controller('firstController',['$scope',function($scope){  
  72.         $scope.users=[  
  73.             {id:10,name:'張三',count:0}  
  74.         ]  
  75.     }]);  

4.執行順序:先編譯,在執行preLink,最后postLink在倒着執行,即compile與pre-link的執行順序是依次執行,但是post-link正好相反.;

 

 

5.參數解釋:

scope

指令用來在其內部注冊監聽器的作用域。

iElement

iElement參數代表實例元素,指使用此指令的元素。在postLink函數中我們應該只操作此元素的子元素,因為子元素已經被鏈接過了。

iAttrs

iAttrs參數代表實例屬性,是一個由定義在元素上的屬性組成的標准化列表,可以在所有指令的鏈接函數間共享。會以JavaScript對象的形式進行傳遞。

controller

controller 參 數 指 向 require 選 項 定 義 的 控 制 器 。 如 果 沒 有 設 置 require 選 項 , 那 么controller參數的值為undefined。

控制器在所有的指令間共享,因此指令可以將控制器當作通信通道(公共API)。如果設置了多個require,那么這個參數會是一個由控制器實例組成的數組,而不只是一個單獨的控制器。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM