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

查看:27
本文介绍了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:

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

  • 从 withing link 使用 postLink 隐式:

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

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

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