AngularJS指令的预链接和后链接功能可以自定义吗? [英] Can AngularJS directive pre-link and post-link functions be customized?
问题描述
在有关AngularJS的文献中,我已经看到了很多有关AngularJS 链接前和链接后功能的信息.
I have seen many references to AngularJS pre- and post-link functions in literature about AngularJS.
但是我不确定它们是否可以定制或是否在框架内部.
I am not sure however whether these can be customized or are internal to the framework.
换句话说,作为AngularJS开发人员,我可以为自己的自定义指令提供我自己的前后链接功能吗?
In other words, as an AngularJS developper, can I provide my own pre and post link functions to my custom directives?
推荐答案
是的,按照@Mikke的回答.总结起来,有四种声明链接函数的方法:
Yes you can, as per @Mikke's answer. To sum up, there are four ways to declare linking functions:
-
从
compile
内部明确指定preLink
和postLink
函数:
From within
compile
specifying bothpreLink
andpostLink
functions explicitly:
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
}
从compile
内部仅隐式返回postLink
:
From within compile
returning only postLink
implicitly:
compile: function compile(tElement, tAttrs, transclude) {
return function postLink( ... ) { ... }
}
从link
内部明确指定preLink
和postLink
:
From within link
specifying both preLink
and postLink
explicitly:
link: {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
从隐式使用postLink
的link
开始:
From withing link
using postLink
implicitly:
link: function postLink( ... ) { ... }
这篇关于AngularJS指令的预链接和后链接功能可以自定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!