获取“错误:[$ injector:unpr]未知提供程序”错误

时间:2015-09-17 09:40:57

标签: javascript angularjs

我正在根据这种做法编写路线解决方案:https://github.com/johnpapa/angular-styleguide#style-y081

但我得到了:

  

错误:[$ injector:unpr]未知提供者:toursServiceProvider< - toursService< - MainController

我的代码如下所示: route-config.js文件

.when('/', {
    templateUrl: 'scripts/partials/home.partial.html',
    controller: 'MainController',
    controllerAs: 'vm',
    resolve: {
        toursService: function(genericData) {
            return genericData.getTours;
        },
        addressService: function(genericData) {
            return genericData.getAddress;
        },
        aboutService: function(genericData) {
            return genericData.getAbout;
        }
    }

})

main.controller.js文件:

(function () {

    'use strict';

    angular
        .module('excursion')
        .controller('MainController', MainController);

    MainController.$inject = ['toursService', 'addressService', 'aboutService'];

    function MainController(toursService, addressService, aboutService) {

        // vm is our capture variable
        var vm = this;

        vm.tour = toursService.getTours;
        vm.address = addressService.getAddress;
        vm.about = aboutService.getAbout;

    }
})();

1 个答案:

答案 0 :(得分:0)

最后我得到它的工作,我需要$ timeout来在控制器加载时提供承诺,这里是更新的代码(只是不同的路由,但逻辑是相同的):

.when('/tours', {
    templateUrl: 'scripts/partials/pages/tours.partial.html',
    controller: 'TourController',
    controllerAs: 'vm',
    resolve: {
        toursService: function(genericData, $timeout) {
            return $timeout(function() {
                return genericData.getTours;
            }, 1000);
        }
    }
})