2014-02-28 54 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(...) { ... } 
    }, 
} 
相關問題