mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-24 13:53:43 +00:00
fix($injector): instantiate returns instance, if non-object value returned from constructor
This commit is contained in:
parent
3173d8603d
commit
776739299b
2 changed files with 32 additions and 6 deletions
|
|
@ -429,12 +429,15 @@ function createInjector(modulesToLoad) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function instantiate(Type, locals){
|
function instantiate(Type, locals) {
|
||||||
var Constructor = function(){},
|
var Constructor = function() {},
|
||||||
instance;
|
instance, returnedValue;
|
||||||
|
|
||||||
Constructor.prototype = Type.prototype;
|
Constructor.prototype = Type.prototype;
|
||||||
instance = new Constructor();
|
instance = new Constructor();
|
||||||
return invoke(Type, instance, locals) || instance;
|
returnedValue = invoke(Type, instance, locals);
|
||||||
|
|
||||||
|
return isObject(returnedValue) ? returnedValue : instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -606,8 +606,12 @@ describe('injector', function() {
|
||||||
|
|
||||||
|
|
||||||
it('should allow constructor to return different object', function() {
|
it('should allow constructor to return different object', function() {
|
||||||
var t = $injector.instantiate(function() { return 'ABC'; });
|
var obj = {};
|
||||||
expect(t).toBe('ABC');
|
var Class = function() {
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
|
expect($injector.instantiate(Class)).toBe(obj);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -616,6 +620,25 @@ describe('injector', function() {
|
||||||
$injector.instantiate(function() { throw 'MyError'; });
|
$injector.instantiate(function() { throw 'MyError'; });
|
||||||
}).toThrow('MyError');
|
}).toThrow('MyError');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should return instance if constructor returns non-object value', function() {
|
||||||
|
var A = function() {
|
||||||
|
return 10;
|
||||||
|
};
|
||||||
|
|
||||||
|
var B = function() {
|
||||||
|
return 'some-string';
|
||||||
|
};
|
||||||
|
|
||||||
|
var C = function() {
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
expect($injector.instantiate(A) instanceof A).toBe(true);
|
||||||
|
expect($injector.instantiate(B) instanceof B).toBe(true);
|
||||||
|
expect($injector.instantiate(C) instanceof C).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('protection modes', function() {
|
describe('protection modes', function() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue