我们正在尝试从 AngularJS 1.0.7 升级到 1.2.1。在 1.0.7 中,我们能够在同一元素中为 ng-include 设置 Controller ,就像这样
<div data-ng-include="'include1.html'" data-ng-controller="MyCtrl1"
MyCtrl1
将可用于 include1.html
中的代码。
当移动到我在 this plunkr 中说明的 AngularJS 1.2.1 时,这会中断.如果将引用的版本更改为 1.0.7,它 works再次。
我有兴趣了解发生了什么变化/为什么会这样。我尝试搜索但找不到任何内容,或者我没有使用正确的术语。
此外,为我的 ng-include
指定 Controller 的正确方法是什么?
最佳答案
为什么不将 ng-controller
从具有 ng-include
的元素移动到模板内部:
index.html:
<div data-ng-include="'include1.html'"></div>
<div data-ng-include="'include2.html'"></div>
include1.html
<div data-ng-controller="MyCtrl1">
<h1>{{Username}}</h1>
</div>
include2.html
<div data-ng-controller="MyCtrl2">
<h1>{{Username}}</h1>
</div>
从 Angular 1.2 版本开始,ngController
和 ngInclude
似乎不能一起使用:
关于javascript - 在 AngularJS 1.2 中,如何为 ng-include 指定 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097198/