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

feat($injector): Allow specifying a decorator on $injector #13103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,16 +642,19 @@ function createInjector(modulesToLoad, strictDi) {
throw $injectorMinErr('unpr', "Unknown provider: {0}", path.join(' <- '));
})),
instanceCache = {},
instanceInjector = (instanceCache.$injector =
protoInstanceInjector =
createInternalInjector(instanceCache, function(serviceName, caller) {
var provider = providerInjector.get(serviceName + providerSuffix, caller);
return instanceInjector.invoke(provider.$get, provider, undefined, serviceName);
}));


forEach(loadModules(modulesToLoad), function(fn) { if (fn) instanceInjector.invoke(fn); });

return instanceInjector.invoke(
provider.$get, provider, undefined, serviceName);
}),
instanceInjector = protoInstanceInjector;

providerCache['$injector' + providerSuffix] = { $get: valueFn(protoInstanceInjector) };
var runBlocks = loadModules(modulesToLoad);
instanceInjector = protoInstanceInjector.get('$injector');
instanceInjector.strictDi = strictDi;
forEach(runBlocks, function(fn) { if (fn) instanceInjector.invoke(fn); });

return instanceInjector;

Expand Down
17 changes: 17 additions & 0 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,23 @@ describe('injector', function() {
expect(log.join('; ')).
toBe('myDecoratedService:input,dependency1; myService:decInput; dec+origReturn');
});


it('should allow for decorators to $inject', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$inject --> $injector (?)
I would probably change it to something like: should allow decorating $injector

injector = createInjector(['ng', function($provide) {
$provide.decorator('$injector', function($delegate) {
return extend({}, $delegate, {get: function(val) {
if (val === 'key') {
return 'value';
}
return $delegate.get(val);
}});
});
}]);

expect(injector.get('key')).toBe('value');
expect(injector.get('$http')).not.toBeUndefined();
});
});
});

Expand Down