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

标签 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>

屏幕上有:

{{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/

相关文章:

javascript - 如何在 AngularJS 中使用 & 替换 &

javascript - 尝试在 AngularJS 的 Controller 之间传递数据

angularjs - 访问同一个工厂中的方法

javascript - 如何修复 ie7/8 中的 JavaScript 错误?

angularjs - 根据条件禁用 ui-sref

javascript - Express React 应用程序在本地运行,但在 heroku 部署时出现错误

PHP/JavaScript 检查 SSL

javascript - 是否可以隐藏 AngularJS 生成的 HTML 属性?

javascript - 在 Bootstrap 时间选择器中使用 24 小时时间

javascript - 右键粘贴后检索数据