AngularJS指令的预链接和后链接功能可以自定义吗? [英] Can AngularJS directive pre-link and post-link functions be customized?

查看:48
本文介绍了AngularJS指令的预链接和后链接功能可以自定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有关AngularJS的文献中,我已经看到了很多有关AngularJS 链接前和链接后功能的信息.

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 customized or are internal to the framework.

换句话说,作为AngularJS开发人员,我可以为自己的自定义指令提供我自己的前后链接功能吗?

In other words, as an AngularJS developper, can I provide my own pre and post link functions to my custom directives?

推荐答案

是的,按照@Mikke的回答.总结起来,有四种声明链接函数的方法:

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

  1. compile内部明确指定preLinkpostLink函数:

  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) { ... }
  }
}

  • compile内部仅隐式返回postLink:

  • From within compile returning only postLink implicitly:

    compile: function compile(tElement, tAttrs, transclude) {
      return function postLink( ... ) { ... }
    }
    

  • link内部明确指定preLinkpostLink:

  • From within link specifying both preLink and postLink explicitly:

    link: {
      pre: function preLink(scope, iElement, iAttrs, controller) { ... },
      post: function postLink(scope, iElement, iAttrs, controller) { ... }
    }
    

  • 从隐式使用postLinklink开始:

  • From withing link using postLink implicitly:

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

  • 这篇关于AngularJS指令的预链接和后链接功能可以自定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆