Javascript 错误:[ng:areq]customerController不是未定义的函数
我是angularjs的初学者,我正在编写一个简单的应用程序来开始使用它。我不断收到此错误Javascript 错误:[ng:areq]customerController不是未定义的函数,javascript,angularjs,angularjs-controller,Javascript,Angularjs,Angularjs Controller,我是angularjs的初学者,我正在编写一个简单的应用程序来开始使用它。我不断收到此错误错误:[ng:areq]customerController不是未定义的函数 我已经试着检查我的代码有什么问题,一切似乎都很好(至少对我来说是这样)。请通读代码并帮助我 Home.html <html ng-app="customerApp"> <head> <title> MyProject</title> </head> <body n
错误:[ng:areq]customerController不是未定义的函数
我已经试着检查我的代码有什么问题,一切似乎都很好(至少对我来说是这样)。请通读代码并帮助我
Home.html
<html ng-app="customerApp">
<head>
<title> MyProject</title>
</head>
<body ng-controller="customerController">
<table border="2">
<thead>
<th ng-click="doSort('name')">Name</th>
<th ng-click="doSort('city')">City</th>
</thead>
<tr ng-repeat="cust in customers | filter: customerFilter | orderBy:sortBy:reverse">
<td>{{ cust.name }}</td>
<td>{{ cust.city }}</td>
</tr>
</table>
<br \>
<br \>
Total customers: {{ customers.length }}
<script src="/scripts/angular.min.js"></script>
<script src="/app/app.js"></script>
<script src="/app/controllers/customerController.js"</script>
</body>
</html>
customerController.js
(function(){
var app = angular.module('customerApp', []);
})();
(function (){
var customerController = function ($scope){
$scope.sortBy='name';
$scope.reverse=false;
$scope.customers= [{name:'Sachin',city:'Dharwad'}, {name:'Karan',city:'Hubli'},{name:'Shishir',city:'Mysore'}];
$scope.doSort= function (propName){
$scope.sortBy= propName;
$scope.reverse= !$scope.reverse;
};
};
customerController.$inject = ['$scope'];
angular.module('customerApp').controller('customerController',customerController );
})();
PS:我已经提到了您不能再将全局函数用作控制器
JavaScript/Angular(实时预览):
HTML:
名称
城市
{{cust.name}
{{cust.city}
总客户:{{customers.length}
您忘记关闭脚本标记的开头部分。这在问题代码中的语法中很明显
改变
<script src="/app/controllers/customerController.js"</script>
OP正在正确注册控制器。你误解了全局函数作为控制器(不推荐)的使用,而不是将函数引用传递给控制器声明,这在脚本标记上是完全有效的,奇怪的是,我一开始尝试了他的代码,但没有成功。我错过了他声明应用程序模块的部分。
<body ng-app="customerApp" ng-controller="customerController">
<table border="2">
<thead>
<th ng-click="doSort('name')">Name</th>
<th ng-click="doSort('city')">City</th>
</thead>
<tr ng-repeat="cust in customers | filter: customerFilter | orderBy:sortBy:reverse">
<td>{{ cust.name }}</td>
<td>{{ cust.city }}</td>
</tr>
</table>
<br \>
<br \> Total customers: {{ customers.length }}
<script src="/app/controllers/customerController.js"</script>
<script src="/app/controllers/customerController.js"></script>