AngularJS:使用 ng-controller 时出现问题
AngularJS : Problem while using ng-controller
ng-controller 问题:错误:[$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=ListDataCtrl
我是 AngularJS 的初学者,我在使用 ng-controller 时遇到问题:
这是我的代码:
<html>
<head>
<title></title>
</head>
<body ng-app>
<div ng-controller="ListDataCtrl">
<div ng-repeat="item in listData">
<p><strong>{{item.name | uppercase}}</strong></p>
<p>{{item.country}}</p>
<p>*********************</p>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script>
function ListDataCtrl($scope){
$scope.listData =[
{"name":"Achraf", "country":"Tunisia"},
{"name":"Maher", "country":"UAE"},
{"name":"Ahmed", "country":"USA"},
{"name":"Issam", "country":"France"}
];
}
</script>
</body>
</html>
the error message is : angular.js:15567 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=ListDataCtrl
控制器未注册错误:
The controller with the name 'ListDataCtrl' is not registered.
从 AngularJS 版本 1.3 开始,您必须使用模块注册您的控制器,而不是将它们公开为全局变量:
documentation
首先您需要为应用程序声明一个 angularJS 模块,然后将控制器注册到该模块,如下所示:
<body ng-app="myApp">
在脚本中,应用声明:
var myApp = angular.module('myApp', []);
控制器设置:
myApp.controller('ListDataCtrl', ['$scope', function($scope) {
...
}
ng-controller 问题:错误:[$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=ListDataCtrl
我是 AngularJS 的初学者,我在使用 ng-controller 时遇到问题: 这是我的代码:
<html>
<head>
<title></title>
</head>
<body ng-app>
<div ng-controller="ListDataCtrl">
<div ng-repeat="item in listData">
<p><strong>{{item.name | uppercase}}</strong></p>
<p>{{item.country}}</p>
<p>*********************</p>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script>
function ListDataCtrl($scope){
$scope.listData =[
{"name":"Achraf", "country":"Tunisia"},
{"name":"Maher", "country":"UAE"},
{"name":"Ahmed", "country":"USA"},
{"name":"Issam", "country":"France"}
];
}
</script>
</body>
</html>
the error message is : angular.js:15567 Error: [$controller:ctrlreg] http://errors.angularjs.org/1.7.8/$controller/ctrlreg?p0=ListDataCtrl
控制器未注册错误:
The controller with the name 'ListDataCtrl' is not registered.
从 AngularJS 版本 1.3 开始,您必须使用模块注册您的控制器,而不是将它们公开为全局变量: documentation
首先您需要为应用程序声明一个 angularJS 模块,然后将控制器注册到该模块,如下所示:
<body ng-app="myApp">
在脚本中,应用声明:
var myApp = angular.module('myApp', []);
控制器设置:
myApp.controller('ListDataCtrl', ['$scope', function($scope) {
...
}