看似简单却行不通:
<html ng-app>
<head>
<script type="text/javascript" src="lib/angular.js"></script>
<script type="text/javascript">
function HelloController($scope) {
$scope.greeting = { text: 'Hello' };
}
</script>
</head>
<body>
<div ng-controller='HelloController'>
<p>{{greeting.text}}, World</p>
</div>
</body>
</html>
屏幕上有:
{{greeting.text}}, World
在控制台中:
Error: [ng:areq] Argument 'HelloController' is not a function, got undefined
http://errors.angularjs.org/1.3.0/ng/areq?p0=HelloController&p1=not%20a%20function%2C%20got%20undefined
at http://localhost:8080/lib/angular.js:80:12
at assertArg (http://localhost:8080/lib/angular.js:1610:11)
at assertArgFn (http://localhost:8080/lib/angular.js:1620:3)
at http://localhost:8080/lib/angular.js:8319:9
at http://localhost:8080/lib/angular.js:7496:34
at forEach (http://localhost:8080/lib/angular.js:343:20)
at nodeLinkFn (http://localhost:8080/lib/angular.js:7483:11)
at compositeLinkFn (http://localhost:8080/lib/angular.js:6991:13)
at compositeLinkFn (http://localhost:8080/lib/angular.js:6994:13)
at compositeLinkFn (http://localhost:8080/lib/angular.js:6994:13)
不明白为什么...除了可能因为我使用 AngularJS v1.3.0
?
最佳答案
是的,是因为1.3.0。 change log将重大更改描述为:
$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.
更改日志继续说明您可以重新启用“全局”函数方法:
Although it's not recommended, you can re-enable the old behavior like this:
angular.module('myModule').config(['$controllerProvider', function($controllerProvider) {
// this option might be handy for migrating old apps, but please don't use it
// in new ones!
$controllerProvider.allowGlobals();
}]);
关于javascript - Brad Green 的书 "AngularJS"中的第一个例子有一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26459083/