javascript - 是否可以在同一个元素上应用多个 AngularJS Controller

标签 javascript angularjs

是否可以在同一个元素上应用多个 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/

相关文章:

javascript - 如何在调试器 :///VM* files in Google Chrome Devtools 中访问和搜索代码

javascript - 如何将组件的 Controller 功能分离到不同的文件中?

javascript - AngularJS 中父 Controller 的范围与共享服务?

asp.net - 让 AngularJS 与使用 Azure AD 保护的 .NET Web API 对话

javascript - 如何在弹出窗口中自动输入用户名,ajax?

javascript - JavaScript 中 JSON.stringify 中的第二个参数

c# - 将html内容保存到数据库

javascript - 在 AngularJS 的表格上使用分页

javascript - Uncaught Error bootstrap 需要在 bootstrap.min.js 处使用 jquery

javascript - 在 html anchor 标记 href 元素中添加一些 javascript