根据Angular版本缺少Angular控制器

时间:2014-12-23 14:16:26

标签: angularjs angularjs-scope

所以我有这个来源:

<!DOCTYPE html>
    <head>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
    <script>
        function commentController($scope) {
            $scope.reportComments = [{ Name:"Frank", Comment:"Booka" }, { Name:"Frank2", Comment:"Booka2" }];
        }
    </script>
    </head>
    <body>
        <div ng-app="" ng-controller="commentController">
            <ul>
              <li ng-repeat="c in reportComments">
                {{ c.Name + ', ' + c.comment }}
              </li>
            </ul>
        </div>      
    </body>
</html>

效果很好。

然后我将AngularJS的版本更改为1.3.0,我收到此错误:

  

错误:[ng:areq] Argument&#39; loadCommentsController&#39;不是一个功能,   未定义   http://errors.angularjs.org/1.3.7/ng/areq?p0=loadCommentsController&p1=not%20a%20function%2C%20got%20undefined       at file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:63:12       在assertArg(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:1575:11)       在assertArgFn(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:1585:3)       在file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:8416:9       在file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:7590:34       at forEach(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:331:20)       at nodeLinkFn(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:7577:11)       在compositeLinkFn(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:7073:13)       at publicLinkFn(file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:6952:30)       在file:/// C:/Users/micah_000/Desktop/comment_testbed/js/angular.js:1449:27

为什么?

我的控制器功能如何定义?

这与此tutorial密切相关。如果一个简单的教程在发行版之间中断,它会使框架看起来像volatile / mercurial /不可靠。

1 个答案:

答案 0 :(得分:5)

在1.3.0中不推荐使用全局函数作为控制器的能力:https://github.com/angular/angular.js/issues/9662。除了简单(和不切实际)的演示之外,这个“功能”并没有真正意义。

您应该在Angular模块上定义控制器:

<!DOCTYPE html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
  <script>
        angular.module('test', [])
          .controller('commentController', function commentController($scope) {
                $scope.reportComments = [
                  { Name:"Frank", Comment:"Booka" }, 
                  { Name:"Frank2", Comment:"Booka2" }
                ];
           });
    </script>
</head>
<body>
    <div ng-app="test" ng-controller="commentController">
        <ul>
          <li ng-repeat="c in reportComments">
            {{ c.Name + ', ' + c.comment }}
          </li>
        </ul>
    </div>      
</body>