我已經在有關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(...) { ... }
},
}