mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-22 09:30:28 +00:00
fix($injector): circular dependency instatiation
This commit is contained in:
parent
fa69d10122
commit
fbcb7fdd14
2 changed files with 27 additions and 1 deletions
|
|
@ -273,7 +273,8 @@ function inferInjectionArgs(fn) {
|
|||
|
||||
|
||||
function createInjector(modulesToLoad) {
|
||||
var providerSuffix = 'Provider',
|
||||
var INSTANTIATING = {},
|
||||
providerSuffix = 'Provider',
|
||||
path = [],
|
||||
loadedModules = new HashMap(),
|
||||
providerCache = {
|
||||
|
|
@ -394,10 +395,14 @@ function createInjector(modulesToLoad) {
|
|||
throw Error('Service name expected');
|
||||
}
|
||||
if (cache.hasOwnProperty(serviceName)) {
|
||||
if (cache[serviceName] === INSTANTIATING) {
|
||||
throw Error('Circular dependency: ' + path.join(' <- '));
|
||||
}
|
||||
return cache[serviceName];
|
||||
} else {
|
||||
try {
|
||||
path.unshift(serviceName);
|
||||
cache[serviceName] = INSTANTIATING;
|
||||
return cache[serviceName] = factory(serviceName);
|
||||
} finally {
|
||||
path.shift();
|
||||
|
|
|
|||
|
|
@ -483,6 +483,27 @@ describe('injector', function() {
|
|||
createInjector([['$injector', myModule]]);
|
||||
}).toThrow('Unknown provider: $injector from ' + myModule);
|
||||
});
|
||||
|
||||
|
||||
it('should throw error when trying to inject oneself', function() {
|
||||
expect(function() {
|
||||
createInjector([function($provide){
|
||||
$provide.factory('service', function(service){});
|
||||
return function(service) {}
|
||||
}])
|
||||
}).toThrow('Circular dependency: service');
|
||||
});
|
||||
|
||||
|
||||
it('should throw error when trying to inject circular dependency', function() {
|
||||
expect(function() {
|
||||
createInjector([function($provide){
|
||||
$provide.factory('a', function(b){});
|
||||
$provide.factory('b', function(a){});
|
||||
return function(a) {}
|
||||
}])
|
||||
}).toThrow('Circular dependency: b <- a');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue