AngularJS 中指令函数的执行顺序 [英] Order of execution of directive functions in AngularJS

查看:34
本文介绍了AngularJS 中指令函数的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指令函数的执行顺序是什么?文档 似乎没有解决这个问题.

What is the order of execution of directive functions? The documentation doesn't seem to address this.

  1. template/templateUrl(已评估)
  2. controllerFn
  3. compileFn
  4. linkFn

答案

来自下面的答案:http://plnkr.co/edit/79iyKSbfxgkzk2Pivuak(plunker 显示嵌套和兄弟指令)

Answer

From answer below: http://plnkr.co/edit/79iyKSbfxgkzk2Pivuak (plunker shows nested and sibling directives)

  1. 模板已解析
  2. compile()(在 compile 中对模板所做的更改扩展到链接函数)
  3. controller()
  4. preLink()
  5. postLink()
  1. Template is parsed
  2. compile() (changes made to the template within compile are proliferated down to linking functions)
  3. controller()
  4. preLink()
  5. postLink()

推荐答案

预链接功能:在链接子元素之前执行.由于编译器链接功能,进行 DOM 转换是不安全的将无法找到正确的链接元素.

Pre-linking function: Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking.

后链接功能:链接子元素后执行.它在 post-linking 函数中进行 DOM 转换是安全的.

Post-linking function: Executed after the child elements are linked. It is safe to do DOM transformation in the post-linking function.

以上摘录来自指令的官方文档.

Above excerpt is taken from the official docs on directives.

所以,为了回答您的问题,后链接/链接功能是您可以安全操作 element.children() 的时间/地点.

So, to answer your question, Post-linking/Link function is when/where you can safely operate on element.children().

这篇关于AngularJS 中指令函数的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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