It's important to note that the technique mentioned here is a workaround, intended for experimentation and educational purposes only!
The inner workings of $injector DI are contained within this file:
https://github.com/angular/angular.js/blob/v1.2.7/src/auto/injector.js
Refer to the createInjector function (line 598 : angularjs 1.2.7)
- The providerCache variable holds what $injector can access in configuration blocks.
- The instanceCache variable holds what $injector can access in other blocks.
function createInjector(modulesToLoad) {
var INSTANTIATING = {},
providerSuffix = 'Provider',
path = [],
loadedModules = new HashMap(),
providerCache = {
$provide: {
provider: supportObject(provider),
factory: supportObject(factory),
service: supportObject(service),
value: supportObject(value),
constant: supportObject(constant),
decorator: decorator
}
},
providerInjector = (providerCache.$injector =
createInternalInjector(providerCache, function() {
throw $injectorMinErr('unpr', "Unknown provider: {0}", path.join(' <- '));
})),
instanceCache = {},
instanceInjector = (instanceCache.$injector =
createInternalInjector(instanceCache, function(servicename) {
var provider = providerInjector.get(servicename + providerSuffix);
return instanceInjector.invoke(provider.$get, provider);
}));
These variables are enclosed within a private scope and cannot be accessed externally.
Unless these lines are added to the createInjector function:
window.providerCache = providerCache;
window.instanceCache = instanceCache;
How to implement it: