AngularJS 1.2 和 1.4 之间的重大变化?
Breaking change from between AngularJS 1.2 and 1.4?
以下代码在 Angular 1.2 中有效,但在 1.4 中中断并出现错误 Error: [ng:areq] Argument 'MyController' is not a function, got undefined。代码来自 "ng-book" 一书,可以在此处找到实际工作示例:http://jsbin.com/uHiVOZo/1/edit?html,output.
有什么变化?
<body>
<div ng-controller="MyController">
{{ clock }}
</div>
<script type="text/javascript">
function MyController($scope) {
$scope.clock = new Date();
var updateClock = function() {
$scope.clock = new Date();
};
setInterval(
function() {
$scope.$apply(updateClock);
},
1000
);
updateClock();
};
</script>
</body>
(替换jsbin中的https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js with https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-rc.2/angular.jslink)
我不知道 Angular 的任何版本允许您在不先创建模块的情况下创建控制器。无论如何,实例化模块并注册控制器可以解决您的问题。 jsbin
将此添加到您在 html 中的应用程序声明:
ng-app="app"
修改脚本:
angular.module('app', []) // this creates a module
function MyController () { ... }
// register controller to module
angular.module('app').controller('MyController', MyController)
参见https://docs.angularjs.org/guide/migration
"从 1.2 迁移到 1.3
控制器
由于 3f2232b5,$controller 将不再在 window 上寻找控制器。在 window 上查找控制器的旧行为最初旨在用于示例、演示和玩具应用程序。我们发现允许全局控制器功能鼓励不良做法,因此我们决定默认禁用此行为。
要迁移,请使用模块注册您的控制器,而不是将它们公开为全局变量:
“
之前:
function MyController() {
// ...
}
之后:
angular.module('myApp', []).controller('MyController', [function() {
// ...
}]);
以下代码在 Angular 1.2 中有效,但在 1.4 中中断并出现错误 Error: [ng:areq] Argument 'MyController' is not a function, got undefined。代码来自 "ng-book" 一书,可以在此处找到实际工作示例:http://jsbin.com/uHiVOZo/1/edit?html,output.
有什么变化?
<body>
<div ng-controller="MyController">
{{ clock }}
</div>
<script type="text/javascript">
function MyController($scope) {
$scope.clock = new Date();
var updateClock = function() {
$scope.clock = new Date();
};
setInterval(
function() {
$scope.$apply(updateClock);
},
1000
);
updateClock();
};
</script>
</body>
(替换jsbin中的https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js with https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-rc.2/angular.jslink)
我不知道 Angular 的任何版本允许您在不先创建模块的情况下创建控制器。无论如何,实例化模块并注册控制器可以解决您的问题。 jsbin
将此添加到您在 html 中的应用程序声明:
ng-app="app"
修改脚本:
angular.module('app', []) // this creates a module
function MyController () { ... }
// register controller to module
angular.module('app').controller('MyController', MyController)
参见https://docs.angularjs.org/guide/migration
"从 1.2 迁移到 1.3 控制器 由于 3f2232b5,$controller 将不再在 window 上寻找控制器。在 window 上查找控制器的旧行为最初旨在用于示例、演示和玩具应用程序。我们发现允许全局控制器功能鼓励不良做法,因此我们决定默认禁用此行为。
要迁移,请使用模块注册您的控制器,而不是将它们公开为全局变量: “
之前:
function MyController() {
// ...
}
之后:
angular.module('myApp', []).controller('MyController', [function() {
// ...
}]);