link 函数在指令编译返回和 link 函数中是否不同?
Are the link functions differernt in directive compile returning and link function?
全部:
[更新]:得到了 this post 的回答(这需要时间阅读,所以我只引用 我的理解的简单回答post 这里):
"Note that if you need a compile function and a link function (or pre
and post link functions), the compile function must return the link
function(s) because the 'link' attribute is ignored if the 'compile'
attribute is defined."
我是 AngularJS 指令的新手,当我学习如何使用 $compile 时,我遇到了一个问题,例如:
我可以定义如下指令:
app.directive("customdir", function(){
return {
restrict:"AE",
compile: function(tmplEL, attrs){
return function(scope, EL, attrs){/*this is the link function*/};
},
link: function(scope, EL, attrs){/*this is also the link function*/}
}
})
我的困惑是:编译函数 return 是一个 link 函数,而我们也可以在指令中定义另一个 link 函数,它们是相同的函数吗 或者它们是 link: function(scope, EL, attrs)
可以覆盖 link function returned from compile
的不同函数?
结果是什么如果我都定义了?
谢谢
这是一个 "either/or"。
您可以在指令声明对象上定义一个 compile
属性,这是一个 returns 一个 link
function/object 的函数 - 然后是 link
属性 被忽略。
compile: function(tElement, tAttrs, transclude){
return {
pre: function prelink(){...},
post: function postlink(){...}
};
}
或者,由于 compile
函数不太常用,您可以只指定 link
属性 - 可以是 post-link函数(如果它是一个函数值)或两者都是 pre-link 和 post-link,如果它是一个具有这些字段的对象值:
link: {
pre: function prelink(scope, element, attrs, ctrls, transclude){
},
post: function postlink(scope, element, attrs, ctrls, transclude){
}
}
或者简单地说:
link: function postlink(scope, element, attrs, ctrls, transclude){
}
更多信息见Angulardocs
全部:
[更新]:得到了 this post 的回答(这需要时间阅读,所以我只引用 我的理解的简单回答post 这里):
"Note that if you need a compile function and a link function (or pre and post link functions), the compile function must return the link function(s) because the 'link' attribute is ignored if the 'compile' attribute is defined."
我是 AngularJS 指令的新手,当我学习如何使用 $compile 时,我遇到了一个问题,例如:
我可以定义如下指令:
app.directive("customdir", function(){
return {
restrict:"AE",
compile: function(tmplEL, attrs){
return function(scope, EL, attrs){/*this is the link function*/};
},
link: function(scope, EL, attrs){/*this is also the link function*/}
}
})
我的困惑是:编译函数 return 是一个 link 函数,而我们也可以在指令中定义另一个 link 函数,它们是相同的函数吗 或者它们是 link: function(scope, EL, attrs)
可以覆盖 link function returned from compile
的不同函数?
结果是什么如果我都定义了?
谢谢
这是一个 "either/or"。
您可以在指令声明对象上定义一个 compile
属性,这是一个 returns 一个 link
function/object 的函数 - 然后是 link
属性 被忽略。
compile: function(tElement, tAttrs, transclude){
return {
pre: function prelink(){...},
post: function postlink(){...}
};
}
或者,由于 compile
函数不太常用,您可以只指定 link
属性 - 可以是 post-link函数(如果它是一个函数值)或两者都是 pre-link 和 post-link,如果它是一个具有这些字段的对象值:
link: {
pre: function prelink(scope, element, attrs, ctrls, transclude){
},
post: function postlink(scope, element, attrs, ctrls, transclude){
}
}
或者简单地说:
link: function postlink(scope, element, attrs, ctrls, transclude){
}
更多信息见Angulardocs