AngularJS:Factory和routeProvider

时间:2016-03-11 19:42:03

标签: angularjs

在“routeProvider”中我解析了“objectStats”,但我的控制器似乎并不理解它。 (我在stackoverflow上发现了几个问题,但没有解决方案解决了我的错误)

我的代码:

var app = angular.module('AppLearning', ['ngRoute','easypiechart']);

app.config(['$routeProvider',function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'index.html',
            controller: 'AppCtrl',
            resolve: {
                objectStats: function(statsFactory) {
                    return statsFactory.getStatsFollow();
                }
            }
        }).
    otherwise({
        redirectTo: '/home'
    });
}]);

app.controller('AppCtrl', ['$scope', '$http', 'objectStats', function ($scope, $http, objectStats) {

    $scope.statsUnfollow = objectStats.data;

    $scope.percent = 65;
    $scope.pieOptions = {
        animate:{
            duration:2000,
            enabled:true
        },
        trackColor: '#e5e5e5',
        barColor: '#3da0ea',  
        scaleColor:false,
        lineWidth:5,
        lineCap:'square'
    };

}]);

app.factory('statsFactory', ['$http', function($http) {
  return {
    //Code edited to create a function as when you require service it returns object 
    //by default so you can't return function directly. That's what understand...
    getStatsFollow: function (type) { 
        var q = $q.defer();
        $http.get('/api/stats/follow').success(function (data) {
        q.resolve(function() {
            var settings = jQuery.parseJSON(data);
            return settings[type];
        });
      });
      return q.promise;
    }
  }
}]);
  

错误:[$ injector:unpr]未知提供者:objectStatsProvider< - objectStats   http://errors.angularjs.org/1.2.9/ $注射器/ unpr?P0 = objectStatsProvider%20%3 C-%20objectStats   minErr /< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:78:12   createInjector / providerCache $注射器< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3546:19   的getService @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3673:39   createInjector / InstanceCache控制$注射器< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3551:28   的getService @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3673:39   调用@ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3700:1   实例@ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3721:23   $ ControllerProvider /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:6772:18   nodeLinkFn /< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:6185:34   的forEach @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:310:11   nodeLinkFn @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:6172:11   compositeLinkFn @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:5636:15   compositeLinkFn @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:5639:13   compositeLinkFn @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:5639:13   publicLinkFn @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:5541:30   自举/ doBootstrap / HTTPS://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:1304:11   $ RootScopeProvider /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:11955:16   $ RootScopeProvider /这$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:12055:18   引导/ doBootstrap /< @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:1302:9   调用@ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:3710:14   自举/ doBootstrap @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:1300:1   自举@ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:1314:1   angularInit @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:1263:5   @ https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js:20555:5   b.Callbacks/c@https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:7852   b.Callbacks/p.fireWith@https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:8658   。就绪@ https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:3264   H + https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:693

0 个答案:

没有答案