fix($injector): ensure $get method invoked with provider context

0d3b69a5f2 broke this by calling $get with an undefined
context, which in strict mode would be undefined. This fixes this by ensuring that the
provider is used as the context, as it was originally.

Closes #9511
Closes #9512
This commit is contained in:
Caitlin Potter
2014-10-09 07:49:49 -04:00
parent 7b102323e9
commit 372fa6993b
2 changed files with 18 additions and 1 deletions

View File

@@ -664,7 +664,7 @@ function createInjector(modulesToLoad, strictDi) {
function enforceReturnValue(name, factory) {
return function enforcedReturnValue() {
var result = instanceInjector.invoke(factory);
var result = instanceInjector.invoke(factory, this, undefined, name);
if (isUndefined(result)) {
throw $injectorMinErr('undef', "Provider '{0}' must return a value from $get factory method.", name);
}

View File

@@ -993,4 +993,21 @@ describe('strict-di injector', function() {
inject(function($test) {});
}).toThrowMinErr('$injector', 'undef');
});
it('should always use provider as `this` when invoking a factory', function() {
var called = false;
function factoryFn() {
called = true;
// jshint -W040
expect(typeof this.$get).toBe('function');
// jshint +W040
return this;
}
module(function($provide) {
$provide.factory('$test', factoryFn);
});
inject(function($test) {});
expect(called).toBe(true);
});
});