我有一个模块'moduleA'有2个服务,而一个(serviceB)依赖于另一个(serviceA)。我试图将serviceB注入一个属于不同模块'foo'的控制器,但是我得到了未知的提供程序错误。以下是我的代码:
module.js
angular.module('moduleA', []);
工厂a.js
angular
.module('moduleA')
.factory('factoryA', factoryA);
factoryA.$inject = ['$q', '$log', '$timeout'];
function factoryA($q, $log, $timeout) {
//Do Stuff
}
工厂b.js
angular
.module('moduleA')
.factory('factoryB', factoryB);
factoryB.$inject = ['factoryA'];
function factoryB(factoryA) {
//Do Stuff
}
FOO-controller.js
angular.module('foo', ['moduleA'])
.controller('fooController', ['factoryB', function(factoryB){
//Do Stuff
})
控制台出错:
generic-console-medium.js:23 2016-11-27 18:49:42.395 - [$injector:unpr] Unknown provider: factoryBProvider <- factoryB <- fooController
http://errors.angularjs.org/1.5.5/$injector/unpr?p0=factoryBProvider%20%3C-%20factoryB%20%3C-%20fooController Error: [$injector:unpr] Unknown provider: factoryBProvider <- factoryB <- fooController
http://errors.angularjs.org/1.5.5/$injector/unpr?p0=factoryBProvider%20%3C-%20factoryB%20%3C-%20fooController
at http://localhost:9001/components/angular/angular.js:68:12
at http://localhost:9001/components/angular/angular.js:4458:19
at Object.getService [as get] (http://localhost:9001/components/angular/angular.js:4611:39)
at http://localhost:9001/components/angular/angular.js:4463:45
at getService (http://localhost:9001/components/angular/angular.js:4611:39)
at injectionArgs (http://localhost:9001/components/angular/angular.js:4635:58)
at Object.invoke (http://localhost:9001/components/angular/angular.js:4657:18)
at $controllerInit (http://localhost:9001/components/angular/angular.js:10115:34)
at nodeLinkFn (http://localhost:9001/components/angular/angular.js:9033:34)
at http://localhost:9001/components/angular/angular.js:9433:13
答案 0 :(得分:2)
这可能听起来很简单,但您是否在项目中包含了 factory-b.js 文件?
我认为您应该修复下面显示的代码。除此之外,一切看起来都很好。
function factoryA($q, $log, $timeout) { //remove the ''
//Do Stuff
}