fix($injector): allow a constructor function to return a function

This change makes `$injector.instantiate` (and thus `$provide.service`) to behave the same as native
`new` operator.
This commit is contained in:
Vojta Jina 2013-10-16 12:14:44 -07:00 committed by Igor Minar
parent dba566a96d
commit c22adbf160
2 changed files with 11 additions and 1 deletions

View file

@ -759,7 +759,7 @@ function createInjector(modulesToLoad) {
instance = new Constructor();
returnedValue = invoke(Type, instance, locals);
return isObject(returnedValue) ? returnedValue : instance;
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
}
return {

View file

@ -788,6 +788,16 @@ describe('injector', function() {
});
it('should allow constructor to return a function', function() {
var fn = function() {};
var Class = function() {
return fn;
};
expect($injector.instantiate(Class)).toBe(fn);
});
it('should handle constructor exception', function() {
expect(function() {
$injector.instantiate(function() { throw 'MyError'; });