是否有可能在相同的元素应用多个AngularJS控制器 [英] Is it possible to apply multiple AngularJS controllers on the same element
问题描述
是否有可能在相同的元素应用多个AngularJS控制器?
Is it possible to apply multiple AngularJS controllers on the same element ?
推荐答案
的没有的,你不能两个控制器适用于同一元素,但你可以申请多个指令。和指令可以有控制器。
No, you can not apply two controllers to the same element, but you can apply multiple directives. And directives can have controllers.
app.directive('myDirective1', function() {
return {
controller: function(scope) {
//directive controller
}
};
});
app.directive('myDirective2', function() {
return {
controller: function(scope) {
//directive controller
}
};
});
和在HTML
<div myDirective1 myDirective2></div>
和如在下面的评论所提到的,这两个控制器可以共享相同的范围,这常常是所期望的效果;两个控制器的人可以申请一个新的范围,但你不能有两个新的范围;
And as mentioned in the comments below, the two controllers could share the same scope, which is often the desired effect; one of the two controller can request a new scope, but you cannot have two new scopes;
有关不允许在两个指令两个孤立范围的原因,是该视图不知道从哪里得到的范围值,如果一个范围的变量在两个独立的指令控制器具有相同的名称
the reason for not allowing two isolated scope on the two directives, is that the view would not know where to get the scope values from, if a scope variable had the same name in the two isolated directive controllers
下面是一个有趣的阅读:<一href=\"http://stackoverflow.com/questions/27485848/why-cant-multiple-directives-ask-for-an-isolated-scope-on-the-same-element\">Why不能将多个指令对同一元素在一个偏僻的范围问?
Here is an interesting read: Why can't multiple directives ask for an isolated scope on the same element?
这篇关于是否有可能在相同的元素应用多个AngularJS控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!