我已经在有关AngularJS的文献中看到许多关于AngularJS 前后链接函数的参考文献。可以定制AngularJS指令预链接和后链接功能吗?
但我不确定这些是可以定制的还是内部的框架。
换句话说,作为AngularJS开发者,我可以提供我自己的前后链接函数到我的自定义指令吗?
我已经在有关AngularJS的文献中看到许多关于AngularJS 前后链接函数的参考文献。可以定制AngularJS指令预链接和后链接功能吗?
但我不确定这些是可以定制的还是内部的框架。
换句话说,作为AngularJS开发者,我可以提供我自己的前后链接函数到我的自定义指令吗?
是的,你可以,根据@ Mikke的答案。归纳起来,有四种方法来声明连接功能:
从内compile
明确指定既preLink
和postLink
功能:
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
明确:
link: {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
从使用postLink
隐含withing link
:
link: function postLink(...) { ... }
是的,你可以提供你自己的预先和后期链接功能。请参阅指令蓝图Angular Docs' Comprehensive Directive API。
{
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
// or
// return function postLink(...) { ... }
},
}