Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c22adbf

Browse files
vojtajinaIgorMinar
authored andcommitted
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.
1 parent dba566a commit c22adbf

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/auto/injector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ function createInjector(modulesToLoad) {
759759
instance = new Constructor();
760760
returnedValue = invoke(Type, instance, locals);
761761

762-
return isObject(returnedValue) ? returnedValue : instance;
762+
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
763763
}
764764

765765
return {

test/auto/injectorSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,16 @@ describe('injector', function() {
788788
});
789789

790790

791+
it('should allow constructor to return a function', function() {
792+
var fn = function() {};
793+
var Class = function() {
794+
return fn;
795+
};
796+
797+
expect($injector.instantiate(Class)).toBe(fn);
798+
});
799+
800+
791801
it('should handle constructor exception', function() {
792802
expect(function() {
793803
$injector.instantiate(function() { throw 'MyError'; });

0 commit comments

Comments
 (0)