我正在尝试依赖注入服务而我收到以下错误
错误:[$ injector:unpr]未知提供者:allStatesServiceProvider< - allStatesService
脚本:PlanDesignController
(function () {
'use strict';
var controllerId = 'PlanDesignController';
angular.module('myApp').controller(controllerId,
['$scope', 'planDesignService', 'allStatesService',
'logger', '$timeout', '$routeParams', '$location', PlanDesignController]);
function PlanDesignController($scope, planDesignService, allStatesService
, logger, $timeout, $routeParams, $location) { }
}
脚本:allStatesService
(function () {
'use strict';
var serviceId = 'allStatesService';
angular.module('myApp')
.factory(serviceId, ['$http','logger', 'appSettings', 'breeze', allStatesService]);
console.log("Gets into AllStatesService.js");
function allStatesService($http, logger, appSettings) {}
}
我该怎么做才能解决此错误?
答案 0 :(得分:1)
我不完全确定这里的问题在哪里。所以我创建了一个plunker来显示你的代码几乎正常工作。检查此working example。一点调整的代码片段
angular.module('myApp', []);
(function () {
'use strict';
var controllerId = 'PlanDesignController';
angular.module('myApp').controller(controllerId,
['$scope', 'planDesignService', 'allStatesService',
'logger', '$timeout', '$routeParams', '$location', PlanDesignController]);
function PlanDesignController($scope, planDesignService, allStatesService
, logger, $timeout, $routeParams, $location)
{ $scope.text = allStatesService.greet()}
})();
(function () {
'use strict';
var serviceId = 'allStatesService';
angular.module('myApp')
.factory(serviceId, ['$http', 'logger', 'appSettings', 'breeze'
, allStatesService]);
function allStatesService($http, logger, appSettings) {
var greeting = function() {
return "Welcome from service"
};
return {
greet : greeting,
};
}
})();
现在主要的变化是服务返回带有名为 greet()
的文本的对象。这在控制器中调用,其结果放在范围内。
此代码段显示,它几乎相同......但是working。这样可以帮助您找出原始代码中的不同之处......并使其运行