错误:[$ controller:ctrlreg] Angular js代码无法正常工作

时间:2017-10-16 04:49:21

标签: angularjs

我是angularJs的新手。我正在读一本书来学习。这里有一个例子没有用。

<!DOCTYPE html>
<html ng-app>

<head>
<script src="js/angular.min.js"></script>
<script>

function MyFirstCtrl($scope) {

var employees = ['Catherine Grant', 'Monica Grant',
'Christopher Grant', 'Jennifer Grant'
];

$scope.ourEmployees = employees;
}

</script>
</head>
<body ng-controller='MyFirstCtrl'>

<h2>Number of Employees: {{ ourEmployees.length}}</h2>
<p ng-repeat="employee in ourEmployees">{{employee}}</p>

</body>
</html>

错误在控制台

中显示如下
  

错误:[$ controller:ctrlreg] http://errors.angularjs.org/1.6.5/$controller/ctrlreg

1 个答案:

答案 0 :(得分:3)

从错误看,你似乎正在使用角度版本1.6。然后控制器不应该是全局的。应该如下,

var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){

});

<强>样本

<!DOCTYPE html>
<html ng-app='testApp'>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<script>
var app = angular.module('testApp',[]);
app.controller('MyFirstCtrl',function($scope){
var employees = ['Catherine Grant', 'Monica Grant',
'Christopher Grant', 'Jennifer Grant'
];

$scope.ourEmployees = employees;
});
 

</script>
</head>
<body ng-controller='MyFirstCtrl'>

<h2>Number of Employees: {{ ourEmployees.length}}</h2>
<p ng-repeat="employee in ourEmployees">{{employee}}</p>

</body>
</html>