AngularJS 中指令函数的执行顺序 [英] Order of execution of directive functions in AngularJS
问题描述
指令函数的执行顺序是什么?文档 似乎没有解决这个问题.
What is the order of execution of directive functions? The documentation doesn't seem to address this.
前
- template/templateUrl(已评估)
- controllerFn
- compileFn
- linkFn
答案
来自下面的答案:http://plnkr.co/edit/79iyKSbfxgkzk2Pivuak(plunker 显示嵌套和兄弟指令)
Answer
From answer below: http://plnkr.co/edit/79iyKSbfxgkzk2Pivuak (plunker shows nested and sibling directives)
- 模板被解析
compile()
(在 compile 中对模板所做的更改扩展到链接函数)controller()
preLink()
postLink()
- Template is parsed
compile()
(changes made to the template within compile are proliferated down to linking functions)controller()
preLink()
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屋!