javascript 注入器已经创建。无法注册模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24900067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
injector already created. can not register a module
提问by Prateek Jain
I am a new bee to Angular JS and was trying to make something out of it in a proper TDD way, but while testing i am getting this error:
我是 Angular JS 的新手,并试图以适当的 TDD 方式利用它来制作一些东西,但是在测试时我遇到了这个错误:
Injector already created, can not register a module!
注入器已创建,无法注册模块!
This is the service i am talking about.
这就是我所说的服务。
bookCatalogApp.service('authorService', ["$resource", "$q", function($resource, $q){
var Author =$resource('/book-catalog/author/all',{},{
getAll : { method: 'GET', isArray: true}
});
var authorService = {};
authorService.assignAuthors = function(data){
authorService.allAuthors = data;
};
authorService.getAll = function(){
if (authorService.allAuthors)
return {then: function(callback){callback(authorService.allAuthors)}}
var deferred = $q.defer();
Author.getAll(function(data){
deferred.resolve(data);
authorService.assignAuthors(data);
});
return deferred.promise;
};
return authorService;
}]);
This is the test for the above service
这是对上述服务的测试
describe("Author Book Service",function(){
var authorService;
beforeEach(module("bookCatalogApp"));
beforeEach(inject(function($injector) {
authorService = $injector.get('authorService');
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
describe("#getAll", function() {
it('should get all the authors for the first time', function() {
var authors = [{id:1 , name:'Prayas'}, {id:2 , name:'Prateek'}];
httpBackend.when('GET', '/book-catalog/author/all').respond(200, authors);
var promise = authorService.getAll();
httpBackend.flush();
promise.then(function(data){
expect(data.length).toBe(2)
});
});
it('should get all the authors as they have already cached', function() {
authorService.allAuthors = [{id:1 , name:'Prayas'}, {id:2 , name:'Prateek'}];
var promise = authorService.getAll();
promise.then(function(data){
expect(data.length).toBe(2)
});
});
});
})
Any help will be appreciated.
任何帮助将不胜感激。
回答by zayquan
If you are mixing calls to module('someApp')
and inject($someDependency)
you will get this error.
如果你是混合电话module('someApp')
和inject($someDependency)
你会得到这个错误。
All your calls to module('someApp')
must occur before your calls to inject($someDependency)
.
您对 的所有调用都module('someApp')
必须发生在对 的调用之前 inject($someDependency)
。
回答by loyalBrown
You're using the injectfunction wrong. As the documentationstates, the injectfunction already instantiates a new instance of $injector. My guess is that by passing $injector as a argument to the inject function you are asking it to instantiate the $injector service twice.
您错误地使用了注入功能。正如文档所述,注入函数已经实例化了一个新的 $injector 实例。我的猜测是,通过将 $injector 作为参数传递给注入函数,您要求它两次实例化 $injector 服务。
Just use injectto pass in the service you want to check. Underneath the covers, injectwill use the $injector service it instantiates to grab services.
只需使用inject传入你要检查的服务即可。在幕后,inject将使用它实例化的 $injector 服务来获取服务。
You can fix this problem by changing the second beforeEachstatement to:
您可以通过将第二个beforeEach语句更改为:
beforeEach(inject(function(_authorService_) {
authorService = _authorService_;
}));
One other thing to note. The argument authorService passed to the injectfunction has been wrapped with '_' so it's name does not hide the variable created within the describefunction. Thats also documented in the inject documentation.
另一件事要注意。传递给注入函数的参数 authorService已用“_”包裹,因此它的名称不会隐藏在描述函数中创建的变量。这也记录在注入文档中。
回答by gilamran
Not sure that this is the cause, but your beforeEach should be like this:
不确定这是原因,但你的 beforeEach 应该是这样的:
beforeEach(function() {
inject(function($injector) {
authorService = $injector.get('authorService');
}
});