Angular js 指令中的后链接与前链接 [英] post link vs pre link in Angular js directives

查看:25
本文介绍了Angular js 指令中的后链接与前链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所述:

http://docs.angularjs.org/guide/directive

Angular js 指令采用两种不同类型的链接函数:

Angular js directives take two different types of link functions:

预链接功能在链接子元素之前执行.进行 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.

此外,如果给定匿名函数,link 的默认键似乎将绑定到 postLink.

Additionally, it appears that the default key of link will bind to postLink if given an anonymous function.

我何时以及为什么要使用预链接功能?

When and why would I ever want to use a pre link function?

推荐答案

当您需要在编译任何子元素之前对作用域进行一些准备时,您唯一需要使用预链接的时候.

The only time you'd want to use a pre link is when you need to perform some preparation on the scope before any child elements compile.

我的团队在编写网格指令时使用了它,以在作用域上定义网格对象并设置其在编译任何子行和单元格对象之前所需的一些属性.

My team has used it when writing a grid directive to define the grid object on the scope and setup some of its properties that are needed before any of the child row and cell objects are compiled.

希望有帮助!

这篇关于Angular js 指令中的后链接与前链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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