错误:[$ injector:unpr]未知提供者:$ scopeProvider< - $ scope< - Pages

时间:2016-02-01 00:53:37

标签: angularjs service pagination factory

我一直在努力包括角度分页并遇到此错误错误:[$ injector:unpr]未知提供程序:$ scopeProvider< - $ scope< - Pages。当我从Pages工厂中删除$ scope时,我收到错误'$ scope not define'但是当我添加$ scope时我收到错误'错误:[$ injector:unpr]未知提供者:$ scopeProvider< - $ scope< - 页面'。我想我会在这里发布一个解决方案,因为我完全卡住了。

是否可以在Pages.js中的Controllers.js中引用$ http调用我的API?

Pages.js

angular.module('pages', []);

defaultPage.factory('Pages', function ($http, $scope) {
    return {
        getPage: function (pageNum) {
            $http.get('https://api.github.com/repos/npm/npm/issues', {
                headers: {
                    'Content-type': 'application/json'
                }
            }).success(function (data) {
                $scope.ctrl.info + pageNum;
            });
        }
    }
});

Controllers.js

/ defaultPage = angular.module('DefaultPage',['ngAnimate','ui.bootstrap']); /

//声明DefaultController的默认页面和依赖项

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);
    }

});

}]); // defaultPage ctrl end

//为我的问题页面和params声明我的IssuesController

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;
        }
    });

}]); //结束

apps.js

var defaultPage = angular.module('GithubIssues', [
     'ngRoute',
     /*'DefaultPage',*/
     'pages'

 ]);

 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'
     });
}]);

filters.js

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

1 个答案:

答案 0 :(得分:0)

$scope在工厂或服务中不可用,仅在控制器

中可用

$scope.ctrl.info + pageNum;即使是$http,也是无效的陈述。

返回then()返回的请求承诺,并在getPage: function (pageNum) { return $http(..... 回调中更新控制器本身的范围

std::vector<A> as({});