AngularJs错误:[ng:areq]

时间:2014-11-22 10:40:56

标签: javascript angularjs angularjs-controller

我收到[ng:areq]错误,可能很傻,但我没看透。

做过一些研究没有发现任何帮助。

演示代码:

<!DOCTYPE html>
<html ng-app>
<head lang="en">
    <meta http-equiv="Content-Type" content="text/javascript; charset=utf-8" />
    <title>Angular Third Example</title>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.min.js"></script>
    <script>
        function NameCtrl($scope) {
          $scope.firstName = 'John';
          $scope.lastName = 'Doe';
        }
   </script>
</head>
<body ng-controller="NameCtrl">
    First Name: <input ng-model="firstName" type="text" />
    <br />

    Last Name: <input ng-model="lastName" type="text" />
    <br />

    Hello {{firstName}} {{lastName}}
</body>
</html>

2 个答案:

答案 0 :(得分:1)

由于角度为1.3.x,因此无法将控制器初始化为窗口函数。相反,您需要明确定义您的应用和控制器:

<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
    <meta http-equiv="Content-Type" content="text/javascript; charset=utf-8" />
    <title>Angular Third Example</title>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.min.js"></script>
    <script>
       angular.module('myApp', []).
       controller('NameCtrl', function($scope) {
          $scope.firstName = 'John';
          $scope.lastName = 'Doe';
       });
   </script>
</head>
<body ng-controller="NameCtrl">
    First Name: <input ng-model="firstName" type="text" />
    <br />

    Last Name: <input ng-model="lastName" type="text" />
    <br />

    Hello {{firstName}} {{lastName}}
</body>
</html>

答案 1 :(得分:1)

请不要使用此类声明。请使用基于模块的声明。

ng-app无法识别需要的名称。

var app = angular.module('app', []);
    app.controller('NameCtrl', ['$scope', function($scope) {
      $scope.firstName = 'John';
      $scope.lastName = 'Doe';
    }]);

演示http://plnkr.co/edit/LOHXR6DrH2o7YtfnCX8G?p=preview