我收到一个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/');
}
}
})();
这是怎么回事吗?谢谢!每个文件
嗯....哇,这里你的语法是超级混乱。如果你打算使用$ inject,只使用$ inject,不要通过数组注入(示例在这里:https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#单独的数据的呼叫)。要说出这里发生的事情是非常困难的。好像你正试图将reportTypeService注入到reportTypeService中,这没有任何意义。 –
无论您创建的服务,控制器等是否已注入到index.html文件中。当angular找不到您要注入的某个依赖项时,通常会发生此类错误。 – pritesh
'.service('reportTypeService',['reportTypeService',reportTypeService]); reportTypeService。$ inject = ['$ http'];'这看起来不太好。只要坚持其中的一个(并顺便说一句,内联注释是错误的)。 – estus