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
:
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) { ... }
}
从 withing link
使用 postLink
隐式:
link: function postLink( ... ) { ... }
这篇关于AngularJS 指令前链接和后链接功能可以自定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!