Skip to content

Commit 550f309

Browse files
feat($injector): add new modules property
The `modules` property is a hash of the modules loaded into the injector at bootstrap time. This can be used to access the module's info.
1 parent 49aba51 commit 550f309

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/auto/injector.js

+24
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ function annotate(fn, strictDi, name) {
180180
* As an array of injection names, where the last item in the array is the function to call.
181181
*/
182182

183+
/**
184+
* @ngdoc property
185+
* @name $injector#modules
186+
* @type {Object}
187+
* @description
188+
* A hash containing all the modules that have been loaded into the
189+
* $injector.
190+
*
191+
* You can use this property to find out information about a module via the
192+
* {@link angular.Module#info `myModule.info(...)`} method.
193+
*
194+
* For example:
195+
*
196+
* ```
197+
* var info = $injector.modules['ngAnimate'].info();
198+
* ```
199+
*
200+
* **Do not use this property to attempt to modify the modules after the application
201+
* has been bootstrapped.**
202+
*/
203+
204+
183205
/**
184206
* @ngdoc method
185207
* @name $injector#get
@@ -673,6 +695,7 @@ function createInjector(modulesToLoad, strictDi) {
673695
instanceInjector = protoInstanceInjector;
674696

675697
providerCache['$injector' + providerSuffix] = { $get: valueFn(protoInstanceInjector) };
698+
instanceInjector.modules = providerInjector.modules = createMap();
676699
var runBlocks = loadModules(modulesToLoad);
677700
instanceInjector = protoInstanceInjector.get('$injector');
678701
instanceInjector.strictDi = strictDi;
@@ -768,6 +791,7 @@ function createInjector(modulesToLoad, strictDi) {
768791
try {
769792
if (isString(module)) {
770793
moduleFn = angularModule(module);
794+
instanceInjector.modules[module] = moduleFn;
771795
runBlocks = runBlocks.concat(loadModules(moduleFn.requires)).concat(moduleFn._runBlocks);
772796
runInvokeQueue(moduleFn._invokeQueue);
773797
runInvokeQueue(moduleFn._configBlocks);

test/auto/injectorSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22

33
/* globals support: false */
44

5+
describe('injector.modules', function() {
6+
it('should expose the loaded module info on the instance injector', function() {
7+
var test1 = angular.module('test1', ['test2']).info({ version: '1.1' });
8+
var test2 = angular.module('test2', []).info({ version: '1.2' });
9+
module('test1');
10+
inject(['$injector', function($injector) {
11+
expect(Object.keys($injector.modules)).toEqual(['ng', 'ngLocale', 'ngMock', 'test1', 'test2']);
12+
expect($injector.modules['test1'].info()).toEqual({ version: '1.1' });
13+
expect($injector.modules['test2'].info()).toEqual({ version: '1.2' });
14+
}]);
15+
});
16+
17+
it('should expose the loaded module info on the provider injector', function() {
18+
var providerInjector;
19+
var test1 = angular.module('test1', ['test2']).info({ version: '1.1' });
20+
var test2 = angular.module('test2', [])
21+
.info({ version: '1.2' })
22+
.provider('test', ['$injector', function($injector) {
23+
providerInjector = $injector;
24+
return { $get: function() {} };
25+
}]);
26+
module('test1');
27+
// needed to ensure that the provider blocks are executed
28+
inject();
29+
30+
expect(Object.keys(providerInjector.modules)).toEqual(['ng', 'ngLocale', 'ngMock', 'test1', 'test2']);
31+
expect(providerInjector.modules['test1'].info()).toEqual({ version: '1.1' });
32+
expect(providerInjector.modules['test2'].info()).toEqual({ version: '1.2' });
33+
});
34+
});
35+
536
describe('injector', function() {
637
var providers;
738
var injector;

0 commit comments

Comments
 (0)