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

Commit 8a38763

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 21630bb commit 8a38763

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/auto/injector.js

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

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

679701
providerCache['$injector' + providerSuffix] = { $get: valueFn(protoInstanceInjector) };
702+
instanceInjector.modules = createMap();
680703
var runBlocks = loadModules(modulesToLoad);
681704
instanceInjector = protoInstanceInjector.get('$injector');
682705
instanceInjector.strictDi = strictDi;
@@ -772,6 +795,7 @@ function createInjector(modulesToLoad, strictDi) {
772795
try {
773796
if (isString(module)) {
774797
moduleFn = angularModule(module);
798+
instanceInjector.modules[module] = moduleFn;
775799
runBlocks = runBlocks.concat(loadModules(moduleFn.requires)).concat(moduleFn._runBlocks);
776800
runInvokeQueue(moduleFn._invokeQueue);
777801
runInvokeQueue(moduleFn._configBlocks);

test/auto/injectorSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
/* globals support: false */
44

5+
describe('injector.modules', function() {
6+
it('should expose the loaded module info', 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(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+
518
describe('injector', function() {
619
var providers;
720
var injector;

0 commit comments

Comments
 (0)