为什么我会收到错误:[$ injector:modulerr] http://errors.angularjs.org/1.6.5/$injector/modulerr?

时间:2017-08-15 13:50:06

标签: javascript html angularjs

我正在尝试使用MVC5和AngularJS从SQL数据库加载数据。后端代码工作正常,我可以获取数据,但它没有填充到我的表中。

我的Index.cshtml看起来像这样:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div ng-app="myapp">
    <div ng-controller="StudentCtrl">
        <table class="table table-striped table-bordered table-hover table-checkable datatable">
            <thead class="grid-top-panel">
                <tr>
                    <th>StudentID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="dataModel in students">
                    <td>{{dataModel.StudentID}}</td>
                    <td>{{dataModel.FirstName}}</td>
                    <td>{{dataModel.LastName}}</td>
                    <td>{{dataModel.Email}}</td>
                </tr>  
            </tbody>
        </table>
    </div>
</div>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/ScriptsNg/Module/app.js"></script>
<script src="~/ScriptsNg/Controller/StudentController.js"></script>
<script src="~/ScriptsNg/Services/StudentService.js"></script> 

app.js:

var app;
(function () {
    'use strict';   
    app = angular.module('myapp', ["ngRoute"]);
})();

StudentService.js:

app.service('StudentService', function ($http) {
    //**********----Get All Record----***************  
    var urlGet = '';
    this.getAll = function (apiRoute) {
        urlGet = apiRoute;
        return $http.get(urlGet);
    }
});

StudentController.js:

app.controller('StudentCtrl', ['$scope', 'StudentService',
    // we inject StudentService  inject because we call getAll method for get all student  
    function ($scope, StudentService) {
        // this is base url   
        var baseUrl = '/api/student/';
        // get all student from databse  
        $scope.getStudents = function () {
            var apiRoute = baseUrl + 'GetStudents/';
            var _student = StudentService.getAll(apiRoute);
            _student.then(function (response) {
                $scope.students = response.data;
            },
                function (error) {
                    console.log("Error: " + error);
                });

        }
        $scope.getStudents();

    }]);

有些帖子建议我在索引页面上添加angular-route脚本,并将ngRouter库注入我的模块。我做了,我仍然得到同样的错误:

  

错误:[$ injector:modulerr] http://errors.angularjs.org/1.6.5/ $ injector / modulerr?

我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

模块,控制器和服务脚本是从错误的文件夹加载的,所以它们没有通过。我将它们更改为从正确的脚本文件夹

引用