加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 我们什么时候使用`preLink`的指令的编译函数?

发布时间:2020-12-17 08:44:56 所属栏目:安全 来源:网络整理
导读:angularjs’指令的编译函数有两个函数:preLink和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
angularjs’指令的编译函数有两个函数:preLink和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.

它告诉我们在preLink不应该做什么,我不知道什么和什么时候应该使用preLink?在大多数时间我只是使用postLink。有什么情况下我们必须使用它吗?

你几乎不需要使用preLink。它的可行情况是,当你需要操作范围内的数据,但不是DOM,在执行链接功能(也是其他指令)之前。

正如jacob评论,你总是可以从控制器做,但有时更适合在指令本身的代码。

有一个很好的文章关于指令如何工作,链接顺序被很好地解释为:http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/

如果你需要一个好的例子为什么有时需要预链接,我建议你看一下angular指令本身的代码。例如https://github.com/angular/angular.js/blob/master/src/ng/directive/ngModel.js

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读