2017-10-16 13 views
2

私は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> 

エラーは、あなたが角度のバージョン1.6を使用していると思われるエラーからコンソールに次のように

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

答えて

2

を示しています。コントローラはグローバルであってはなりません。それは、次の

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

}); 

DEMO

<!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>

ようにする必要があります
関連する問題