错误: [ng:areq] 客户控制器不是未定义的函数

Error: [ng:areq] customerController not a function got undefined

本文关键字:未定义 函数 控制器 客户 ng areq 错误      更新时间:2023-09-26

我是angularjs的初学者,我正在编写一个简单的应用程序来开始使用它。我一直收到此错误Error: [ng:areq] customerController not a function got undefined

我试图检查我的代码有什么问题,一切似乎都很好(至少对我来说)。请浏览代码并帮助我

首页.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>

应用.js

(function(){
    var app = angular.module('customerApp', []);
})();

客户控制器.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:我已经提到了这个问题

您忘了关闭脚本标签的开头部分。在问题代码中的语法突出显示中很明显

改变

<script src="/app/controllers/customerController.js"</script>

<script src="/app/controllers/customerController.js"></script>

不能再将全局函数用作控制器。

JavaScript/Angular(Live Preview 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 }}