Angular JS 1.5.X显示控制器出错

时间:2016-09-03 18:42:18

标签: javascript angularjs web

我正在编写一个简单的控制器示例,其中包含angular.js版本1.5.X,这有一些值得注意的问题。问题只出现在Angular JS 1.5.X版本中,但是我已经使用Angular JS V 1.2.X测试了代码,它按预期工作。我不确定我是否以过时的方式做某事?我无法理解控制台日志。

我的代码 -

<!doctype html>
<html lang="en" ng-app="">
<head>
  <meta charset="UTF-8">
  <title>Angular Demo</title>
  <script src="lib/angular/angular.min.1.5.X.js"></script>
</head>
<body>

<div ng-controller = "MyController">
  <h1>{{author.name}}</h1>
  <p>{{ author.title + ', ' + author.company }}</p>
</div>

<script>
function MyController($scope) {
  $scope.author = {
    'name' : 'Ray Villalobos',
    'title' : 'Staff Author',
    'company' : 'Lynda.com'
  }
}
</script>
</body>
</html>

结果:

{{author.name}}

{{author.title +','+ author.company}}

控制台日志:

angular.min.1.5.X.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:6:412
    at sb (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:23:18)
    at Pa (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:23:105)
    at file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:89:310
    at ag (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:72:419)
    at p (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:64:262)
    at g (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:58:481)
    at g (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:58:498)
    at g (file:///G:/Programming-Joy/Web/JavaScript/JavaScript%20Essential%20Training%20Lynda/AngularJs/lib/angular/angular.min.1.5.X.js:58:498)

1 个答案:

答案 0 :(得分:1)

当Angular高于1.3时,你需要像这样定义控制器,

 app.controller("MyController" , function($scope)
   {
       $scope.author = {
      'name' : 'Ray Villalobos',
      'title' : 'Staff Author',
      'company' : 'Lynda.com'
      }
   }); 

DEMO APP