Can AngularJS directive pre-link and post-link functions be customized?

前端 未结 2 908
盖世英雄少女心
盖世英雄少女心 2021-02-03 10:16

I have seen many references to AngularJS pre- and post-link functions in literature about AngularJS.

I am not sure however whether these can be customiz

2条回答
  •  孤街浪徒
    2021-02-03 10:49

    Yes you can, as per @Mikke's answer. To sum up, there are four ways to declare linking functions:

    1. From within compile specifying both preLink and postLink functions explicitly:

      compile: function compile(tElement, tAttrs, transclude) {
        return {
          pre: function preLink(scope, iElement, iAttrs, controller) { ... },
          post: function postLink(scope, iElement, iAttrs, controller) { ... }
        }
      }
      
    2. From within compile returning only postLink implicitly:

      compile: function compile(tElement, tAttrs, transclude) {
        return function postLink( ... ) { ... }
      }
      
    3. From within link specifying both preLink and postLink explicitly:

      link: {
        pre: function preLink(scope, iElement, iAttrs, controller) { ... },
        post: function postLink(scope, iElement, iAttrs, controller) { ... }
      }
      
    4. From withing link using postLink implicitly:

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

提交回复
热议问题