Brad Green的书"AngularJS"的第一个例子有一个错误

Nik*_*yev 1 javascript angularjs

它似乎很简单但不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

在屏幕上:

{{greeting.text}}, World
Run Code Online (Sandbox Code Playgroud)

并在控制台中:

    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) 
Run Code Online (Sandbox Code Playgroud)

看不出为什么......除了因为我使用AngularJS v1.3.0

Doc*_*Max 6

是的,这是因为1.3.0.在更改日志描述了重大更改为:

$ controller将不再在窗口中查找控制器.查看控制器窗口的旧行为最初打算用于示例,演示和玩具应用程序.我们发现允许全局控制器功能鼓励不良做法,因此我们决定默认禁用此行为.

更改日志继续说明您可以通过以下方式重新启用"全局"功能方法:

虽然不推荐,但您可以重新启用旧的行为,如下所示:

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();
}]);
Run Code Online (Sandbox Code Playgroud)