我是angularjs的初学者,我正在编写一个简单的应用程序来开始使用它。我一直收到此错误Error: [ng:areq] customerController not a function got undefined
我试图检查我的代码是否有错,一切似乎都没问题(至少对我而言)。请仔细阅读代码并帮助我
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>
app.js
(function(){
var app = angular.module('customerApp', []);
})();
customerController.js
(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:我已经提到了this question
答案 0 :(得分:2)
您忘记关闭脚本标记的开头部分。在问题代码
中的语法高亮显示中很明显更改
<script src="/app/controllers/customerController.js"</script>
要
<script src="/app/controllers/customerController.js"></script>
答案 1 :(得分:0)
您不能再使用全局功能作为控制器。
JavaScript / Angular(实时预览http://codepen.io/larryjoelane/pen/ZQqVVY):
var app = angular.module("customerApp", []);
app.controller("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;
};
});
HTML:
<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 }}