AngularJS - 未捕获错误:[$ injector:nomod]

时间:2016-03-24 21:26:51

标签: javascript angularjs

在控制器文件夹下,我创建了CustomerController.js:

(function () {
    angular.module('CustomerApp').controller('CustomerController', function ($scope) {

        $scope.customers = [
            {name: 'Sumit', age: 27, salary: 27000, address: 'Katwaria Sarai', joined: '2016-09-21'},
            {name: 'Ankit', age: 25, salary: 34000, address: 'Katwaria Sarai', joined: '2016-06-22'},
            {name: 'Kuldeep', age: 26, salary: 27000, address: 'Sangam Vihar', joined: '2016-01-21'},
            {name: 'Ashutosh', age: 27, salary: 27000, address: 'Vasundara', joined: '206-09-21'},
            {name: 'Rashid', age: 17, salary: 17000, address: 'Pune', joined: '2016-09-21'},
            {name: 'Shifali', age: 27, salary: 17000, address: 'Katwaria Sarai', joined: '2016-09-21'},
            {name: 'Anita', age: 27, salary: 19000, address: 'Katwaria Sarai', joined: '2016-09-17'},
            {name: 'Deepak', age: 27, salary: 29000, address: 'Mahipalpur', joined: '2016-09-15'},
            {name: 'Abhishek', age: 27, salary: 27000, address: 'Darjeeling', joined: '2016-09-19'},
            {name: 'Binetesh', age: 27, salary: 30000, address: 'Kishangar', joined: '2016-09-23'},
            {name: 'Devendra', age: 27, salary: 9000, address: 'Bangalore', joined: '2016-09-22'},
            {name: 'Himanshu', age: 27, salary: 15000, address: 'Sangam Vihar', joined: '2016-03-29'},
            {name: 'Parry', age: 27, salary: 12000, address: 'Siri', joined: '2016-01-07'},
            {name: 'Amit', age: 27, salary: 17000, address: 'Katwaria Sarai', joined: '2016-01-11'}
        ];
    });
}()); 

` 在模块文件夹下我有一个文件app.js.代码如下:

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

这是我的index.jsp文件,描述如下:

<html ng-app="CustomerApp">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link rel="sylesheet" href="css/mystyle.css">
    <script src="js/angular.js"></script>
    <title>Customer Page</title>
</head>
<body ng-controller="CustomerController">
<h2>Customer Data</h2>
Filter: <input type="text">
<br><br>
<table border="1">
    <tr>
        <th>NAME</th>
        <th>AGE</th>
        <th>SALARY</th>
        <th>ADDRESS</th>
        <th>JOINED</th>
    </tr>
    <tr ng-repeat="cust in customers">
        <td>{{cust.name | uppercase}}</td>
        <td>{{cust.age | number}}</td>
        <td>{{cust.salary | currency}}</td>
        <td>{{cust.address}}</td>
        <td>{{cust.joined | date}}</td>
    </tr>
</table>
<script src="controller/CustomerController.js"></script>
<script src="module/app.js"></script>
</body>
</html>

我正在使用版本为1.5.0的angular.js文件

当我运行此代码时,它会将此作为错误提供:

  

未捕获错误:[$ injector:nomod]模块'CustomerApp'不可用或未加载

请帮帮我。我已经尝试了很多但仍然没有解决方案。

1 个答案:

答案 0 :(得分:2)

app.js之前加载controller.js

因此CustomerAppmodule angular模块可用于controller.js将控制器组件的模块扩展到CustomerApp模块。

<script src="module/app.js"></script>
<script src="controller/CustomerController.js"></script>

Demo Here