2014-02-28 53 views
12

我已经在有关AngularJS的文献中看到许多关于AngularJS 前后链接函数的参考文献。可以定制AngularJS指令预链接和后链接功能吗?

但我不确定这些是可以定制的还是内部的框架。

换句话说,作为AngularJS开发者,我可以提供我自己的前后链接函数到我的自定义指令吗?

回答

31

是的,你可以,根据@ Mikke的答案。归纳起来,有四种方法来声明连接功能:

  1. 从内compile明确指定既preLinkpostLink功能:

    compile: function compile(tElement, tAttrs, transclude) { 
        return { 
        pre: function preLink(scope, iElement, iAttrs, controller) { ... }, 
        post: function postLink(scope, iElement, iAttrs, controller) { ... } 
        } 
    } 
    
  2. compile从只返回postLink含蓄:

    compile: function compile(tElement, tAttrs, transclude) { 
        return function postLink(...) { ... } 
    } 
    
  3. link同时指定preLinkpostLink明确:

    link: { 
        pre: function preLink(scope, iElement, iAttrs, controller) { ... }, 
        post: function postLink(scope, iElement, iAttrs, controller) { ... } 
    } 
    
  4. 从使用postLink隐含withing link

    link: function postLink(...) { ... } 
    
4

是的,你可以提供你自己的预先和后期链接功能。请参阅指令蓝图Angular Docs' Comprehensive Directive API

{ 
    compile: function compile(tElement, tAttrs, transclude) { 
     return { 
      pre: function preLink(scope, iElement, iAttrs, controller) { ... }, 
      post: function postLink(scope, iElement, iAttrs, controller) { ... } 
     } 
     // or 
     // return function postLink(...) { ... } 
    }, 
} 
相关问题