在AngularJS的指令功能执行顺序 [英] Order of execution of directive functions in AngularJS

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

问题描述

什么是指令功能执行的顺序?该文档似乎并没有解决这个问题。


  1. 模板/ templateUrl(评估)

  2. controllerFn

  3. compileFn

  4. linkFn

通过回答如下:的 http://plnkr.co/edit/79iyKSbfxgkzk2Pivuak (plunker显示嵌套和兄弟指令)


  1. 模板被解析

  2. 编译()(编译内模板所做的更改激增到连接功能)

  3. 控制器()

  4. $ P $砰砰()

  5. postLink()


解决方案

  

pre-链接功能:子元素链接之前执行。
  没有安全做DOM转型,因为编译器链接功能
  将无法找到正确的元素联系起来。


  
  

后链接功能:子元素被链接后执行。它
  是安全的DOM转型后链接功能。


以上摘录从指令官方文档拍摄的。

因此​​,要回答你的问题,的张贴联/ Link功能的是,当/在那里你可以安全地运行element.children()。

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

Ex

  1. template / templateUrl (is evaluated)
  2. controllerFn
  3. compileFn
  4. linkFn

Answer

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

  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()

解决方案

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 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.

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

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

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