• 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,那么这个参数会是一个由控制器实例组成的数组,而不只是一个单独的控制器。

  • 相关阅读:
    Codeforces 876C Classroom Watch:枚举
    Codeforces 876B Divisiblity of Differences:数学【任意两数之差为k的倍数】
    BZOJ 3943 [Usaco2015 Feb]SuperBull:最大生成树
    BZOJ 3391 [Usaco2004 Dec]Tree Cutting网络破坏:dfs【无根树 节点分枝子树大小】
    markdown常用数学符号小结
    BZOJ3211花神游历各国-线段树&树状数组-(HDU4027同类型)
    一维二维树状数组写法总结
    hdu4352-XHXJ's LIS状压DP+数位DP
    常用Git命令以及出现的状况ing
    bitset简单用法
  • 原文地址:https://www.cnblogs.com/yerikm/p/6425345.html
Copyright © 2020-2023  润新知