AngularJS未捕获错误:[$ injector:modulerr]无法实例化模块

时间:2016-01-31 05:23:42

标签: javascript jquery angularjs twitter-bootstrap pagination

我正在通过相关帖子找到这个有角度的,jquery,bootstrap分页教程http://jsfiddle.net/ef0d9zuk/1/。教程示例工作但我收到错误消息未捕获错误:[$ injector:modulerr]无法实例化模块GithubIssues由于: 错误:[$ injector:modulerr]由于以下原因导致无法实例化模块页面: 错误:[$ injector:nomod]模块“页面”不可用!您要么错误拼写了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

有谁能告诉我这里我做错了什么?

 var defaultPage = angular.module('GithubIssues', [
     'ngRoute',
     'DefaultPage'
 ]);

 defaultPage.config(['$routeProvider', function ($routeProvider) {
     $routeProvider.
     when('/default', {
         templateUrl: 'partials/default.html',
         controller: 'DefaultController'
     }).
     when('/issues/:issueId', {
         templateUrl: 'partials/issues.html',
         controller: 'IssuesController'
     }).
     otherwise({
         redirectTo: '/default'
     });
}]);



    // declaring my DefaultController for default page and params 

    defaultPage = angular.module('DefaultPage', ['pages']);


    angular.module('pages', []).factory('Pages', function () {
        return {
            getPage: function (pageNum) {
                var pageData = $scope.ctrl.info + pageNum;
            }
            return pageData;
        }
    });

    defaultPage.controller('DefaultController', ['$scope', '$http', 'Pages', function ($scope, $http, Pages) {
        $scope.data = {};

        var url = 'https://api.github.com/repos/npm/npm/issues'

        $http.get(url, {
            headers: {
                'Content-type': 'application/json'
            }
        }).success(function (data) {
            $scope.ctrl.info = data;

            $scope.currentPage = 0;
            $scope.pageSize = 10;

            $scope.numberOfPages = function () {
                return 25;
            };

            $scope.data = Pages.getPage($scope.currentPage);
            $scope.getPage = function (pageNum) {
                $scope.data = Pages.getPage(pageNum);
            }

            /* $scope.itemsPerPage = 26;
             $scope.currentPage = 1;*/

            /*$scope.makeTodos = function() {
                $scope.GithubIssues = [];
                for (var i = 1; i <= $scope.ctrl.info.length; i++) {
                    $scope.GithubIssues.push({ $scope.ctrl.info, done:false});
                }
            };*/

            /* $scope.figureOutTodosToDisplay = function() {
                var begin = (($scope.currentPage - 1) * $scope.itemsPerPage);

                var end = begin + $scope.itemsPerPage;

                $scope.ctrl.info = $scope.ctrl.info.slice(begin, end);
                console.log(data);
    ;        };*/

            /*$scope.makeTodos();*/

            /*$scope.figureOutTodosToDisplay();

            $scope.pageChanged = function() {
                $scope.figureOutTodosToDisplay();
            };*/
        });
    }]); //defaultPage ctrl end



    defaultPage.filter('startFrom', function () {
        return function (input, start) {
            start = +start;
            return input.slice(start);
        }
    });


    // declaring my IssuesController for my issues page and params

    defaultPage.controller('IssuesController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
        $http.get('https://api.github.com/repos/npm/npm/issues')
            .success(function (data) {
                $scope.ctrl = data;
                $scope.whichIssue = $routeParams.issueId;

                //Issue page previous issue button 
                if ($routeParams.issueId > 0) {
                    $scope.prevIssue = Number($routeParams.issueId) - 1;
                } else {
                    $scope.prevIssue = $scope.ctrl.length - 1;
                }

                //Issue page next issue button
                if ($routeParams.issueId < $scope.ctrl.length - 1) {
                    $scope.nextIssue = Number($routeParams.issueId) + 1;
                } else {
                    $scope.nextIssue = 0;
                }
            });

    }]); // end

2 个答案:

答案 0 :(得分:1)

删除行

defaultPage = angular.module('DefaultPage', ['pages']);

并更改&#39; DefaultPage&#39;依赖于&#39;页面&#39;在

 var defaultPage = angular.module('GithubIssues', [
 'ngRoute',
 'DefaultPage'
 ]);

将解决依赖性错误。出错的原因是defaultPage name对两个不同的模块使用了两次。

答案 1 :(得分:-1)

在注册之前似乎要注入依赖

更改顺序:

defaultPage = angular.module('DefaultPage', ['pages']);


angular.module('pages', []).factory('Pages', function () {
    return {
        getPage: function (pageNum) {
            var pageData = $scope.ctrl.info + pageNum;
        }
        return pageData;
    }
});

angular.module('pages', []).factory('Pages', function () {
    return {
        getPage: function (pageNum) {
            var pageData = $scope.ctrl.info + pageNum;
        }
        return pageData;
    }
});

defaultPage = angular.module('DefaultPage', ['pages']);