1
在Angular JS documentation爲$編譯它指出:等同於僅使用後鏈接函數返回對象?
一個編譯函數可以具有可以是 功能或對象的返回值。
返回一個(後鏈接)函數 - 相當於當 編譯函數爲空時,通過配置對象的鏈接屬性註冊 鏈接函數。
返回經由預註冊併發布 特性功能(多個)的對象 - 可用於控制何時邏輯函數應該是 在鏈接階段調用。請參閱以下有關預鏈接和後鏈接功能的信息。
基於The nitty-gritty of compile and link functions inside AngularJS directives作爲一個編譯函數返回一個對象的例子:
function createDirective(name){
return function(){
return {
restrict: 'E',
compile: function(tElem, tAttrs){
console.log(name + ': compile');
return {
post: function(scope, iElem, iAttrs){
console.log(name + ': post link');
}
}
}
}
}
}
我的文檔的解釋是,返回功能是允許的,它假定後鏈接功能,即以下內容相同:
function createDirective(name){
return function() {
return {
restrict: 'E',
compile: function(tElem, tAttrs){
console.log(name + ': compile');
return function(scope, iElem, iAttrs){
console.log(name + ': post link');
}
}
}
}
}
這是正確的嗎?
我可以看到相同的[文檔](https://docs.angularjs.org/api/ng/service/$compile)狀態:_A編譯函數可以有一個返回值,它可以是函數或對象._這正是你所說的。謝謝。 – Jack
我從來沒有使用過一個對象作爲自己的回報,但也許它有一個用處。 – SoluableNonagon