Why doesn't the latest version of angular support global controller functionality?
With newer AngularJs 1.3.0, no, but with 1.2.9 the old version works. What's new in the new version?
<html ng-app>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div ng-controller = "MyController">
<h1>{{author.name}}</h1>
<p>{{ author.title }}</p>
</div>
<script>
function MyController($scope) {
$scope.author = {
'name': 'Nagy Dávid',
'title': 'Demo',
}
}
</script>
</body>
</html>
+3
source to share
1 answer
There is a breaking change in angular v1.3.0-beta.15 so that by default angular will no longer look for controllers in the window. See 3f2232b5 for details .
With the exception of simple demos, it is not helpful to use globals for controller constructors. This adds a new method to `$controllerProvider` to re-enable the old behavior, but disables this feature by default. BREAKING CHANGE: `$controller` will no longer look for controllers on `window`. The old behavior of looking on `window` for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default. To migrate, register your controllers with modules rather than exposing them as globals:
So to make your example work without creating your own module (but not recommended), you can add this code to the script tag at the bottom:
angular.module('ng').config(function ($controllerProvider) {
$controllerProvider.allowGlobals();
});
For a working example, see the plunker below.
Plunker example: http://plnkr.co/edit/xdlfJRpH8lHzNvqyQ0no?p=preview
+10
source to share