mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-13 17:02:23 +08:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user