2017-02-26 106 views
0

我收到一个angular.js:14362 Error: [$injector:unpr] Unknown provider: reportTypeServiceProvider <- reportTypeService <- ReportTypeController错误。

我发现了一堆关于这个东西,但我仍然无法弄清楚的问题是在我的代码。那就是:

pitchviz.js:

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz', [ 
     'pitchviz.config', 
     'pitchviz.routes', 
     'pitchviz.report-types' 
    ]); 

    angular 
    .module('pitchviz.config', []); 

    angular 
    .module('pitchviz.routes', ['ngRoute']); 

    angular 
    .module('pitchviz') 
    .run(run); 

    run.$inject = ['$http']; 

    function run($http) { 
    $http.defaults.xsrfHeaderName = 'X-CSRFToken'; 
    $http.defaults.xsrfCookieName = 'csrftoken'; 
    } 
})(); 

pitchviz.config.js:

(function() { 
'use strict'; 

    angular 
    .module('pitchviz.config') 
    .config(config); 

    config.$inject = ['$locationProvider']; 

    function config($locationProvider) { 
    $locationProvider.html5Mode(true); 
    $locationProvider.hashPrefix('!'); 
    } 
})(); 

pitchviz.routes.js:

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.routes') 
    .config(config); 

    config.$inject = ['$routeProvider']; 

    function config($routeProvider) { 
    $routeProvider.when('/', { 
     controller: 'ReportTypeController', 
     controllerAs: 'vm', 
     templateUrl: '/static/templates/report-types/report-types.html' 
    }).otherwise('/'); 
    } 
})(); 

报告类型/报告-types.modules.js:

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types', [ 
     'pitchviz.report-types.controllers', 
     'pitchviz.report-types.services' 
    ]); 

    angular 
    .module('pitchviz.report-types.controllers', []); 

    angular 
    .module('pitchviz.report-types.services', []); 
})(); 

报告类型/控制器/报告-type.controller.js:

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types.controllers') 
    .controller('ReportTypeController', ['reportTypeService', ReportTypeController]); 

    ReportTypeController.$inject = ['reportTypeService']; 

    function ReportTypeController(reportTypeService) { 
    var vm = this; 

    vm.reportType = undefined; 

    activate(); 

    function activate() { 

     reportTypeService.get().then(reportTypeSuccess, reportTypeError); 

     function accountSuccess(data, status, headers, config) { 
     console.log(data); 
     vm.reportType = data.data; 
     } 

     function reportTypeError(data, status, headers, config) { 
     console.log(data); 
     console.log(status); 
     } 
    } 
    } 
})(); 

报告类型/服务/报告-type.service.js:

(function() { 
    'use strict'; 

    angular 
    .module('pitchviz.report-types.services') 
    .service('reportTypeService', ['reportTypeService', reportTypeService]); 

    reportTypeService.$inject = ['$http']; 

    function reportTypeService($http) { 
    var reportTypeService = { 
     get: get 
    }; 

    return reportTypeService; 

    function get() { 
     console.log("service"); 
     return $http.get('/api/report-types/'); 
    } 
    } 
})(); 

这是怎么回事吗?谢谢!每个文件

+2

嗯....哇,这里你的语法是超级混乱。如果你打算使用$ inject,只使用$ inject,不要通过数组注入(示例在这里:https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#单独的数据的呼叫)。要说出这里发生的事情是非常困难的。好像你正试图将reportTypeService注入到reportTypeService中,这没有任何意义。 –

+0

无论您创建的服务,控制器等是否已注入到index.html文件中。当angular找不到您要注入的某个依赖项时,通常会发生此类错误。 – pritesh

+0

'.service('reportTypeService',['reportTypeService',reportTypeService]); reportTypeService。$ inject = ['$ http'];'这看起来不太好。只要坚持其中的一个(并顺便说一句,内联注释是错误的)。 – estus

回答

0

一个模块状图案,但这里的问题是,它使用不当这里。应在其文件中定义子模块,例如report-type.controller.js:

angular 
    .module('pitchviz.report-types.controllers', []) 
    .controller('ReportTypeController', ...) 

不在父模块文件report-types.modules.js中。

只有这样,它可以保证模块单位那里,如果模块存在,并且模块文件可以以任意顺序加载。

0

reportTypeServicepitchviz.report-types.services模块。但ReportTypeControllerpitchviz.report-types.controllers模块中。我认为pitchviz.report-types.controllers无法识别reportTypeService,因为它位于单独的模块中。如果您想访问pitchviz.report-types.controllers模块中的reportTypeService。您必须将reportTypeService添加到pitchviz.report-types.controllers模块或将pitchviz.report-types.services模块添加到pitchviz.report-types.controllers模块。

angular.module("pitchviz.report-types.controllers", ["pitchviz.report-types.services"]); 
+0

不是。一旦pitchviz.report-types被加载到主模块中,reportTypeService属于哪个模块并不重要。这在这里不会导致错误。但是,如您所示,定义模块依赖关系很有用,这是一个很好的做法。 – estus

0

观察:这里,问题是在report-types/controllers/report-type.controller.js因为你是一个confusable语法那里。

  • 无需使用注入了$inject服务,你已经注入,使用数组。

删除此:ReportTypeController.$inject = ['reportTypeService'];

只需使用这样的:

.controller('ReportTypeController', ['reportTypeService', ReportTypeController]); 

function ReportTypeController(reportTypeService) { 
    .... 
    .... 
    .... 
} 
相关问题