是否可以在同一个元素上应用多个 AngularJS Controller ?
最佳答案
不,您不能将两个 Controller 应用于同一个元素,但您可以应用多个指令。指令可以有 Controller 。
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>
正如下面评论中提到的,两个 Controller 可以共享相同的范围,这通常是预期的效果;两个 Controller 之一可以请求一个新的范围,但你不能有两个新的范围;
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
这是一篇有趣的读物:Why can't multiple directives ask for an isolated scope on the same element?
关于javascript - 是否可以在同一个元素上应用多个 AngularJS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28638823/