-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($injector): ability to load new modules after bootstrapping #16224
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,7 +337,46 @@ function annotate(fn, strictDi, name) { | |
* | ||
* @returns {Array.<string>} The names of the services which the function requires. | ||
*/ | ||
|
||
/** | ||
* @ngdoc method | ||
* @name $injector#loadNewModules | ||
* | ||
* @description | ||
* | ||
* **This is a dangerous API, which you use at your own risk!** | ||
* | ||
* Add the specified modules to the current injector. | ||
* | ||
* This method will add each of the injectables to the injector and execute all of the config and run | ||
* blocks for each module passed to the method. | ||
* | ||
* If a module has already been loaded into the injector then it will not be loaded again. | ||
* | ||
* * The application developer is responsible for loading the code containing the modules; and for | ||
* ensuring that lazy scripts are not downloaded and executed more often that desired. | ||
* * Previously compiled HTML will not be affected by newly loaded directives, filters and components. | ||
* * Modules cannot be unloaded. | ||
* | ||
* You can use {@link $injector#modules `$injector.modules`} to check whether a module has been loaded | ||
* into the injector, which may indicate whether the script has been executed already. | ||
* | ||
* ## Example | ||
* | ||
* Here is an example of loading a bundle of modules, with a utility method called `getScript`: | ||
* | ||
* ```javascript | ||
* app.factory('loadModule', function($injector) { | ||
* return function loadModule(moduleName, bundleUrl) { | ||
* return getScript(bundleUrl).then(function() { $injector.loadNewModules([moduleName]); }); | ||
* }; | ||
* }) | ||
* ``` | ||
* | ||
* @param {Array<String|Function|Array>=} mods an array of modules to load into the application. | ||
* Each item in the array should be the name of a predefined module or a (DI annotated) | ||
* function that will be invoked by the injector as a `config` block. | ||
* See: {@link angular.module modules} | ||
*/ | ||
|
||
|
||
/** | ||
|
@@ -701,6 +740,11 @@ function createInjector(modulesToLoad, strictDi) { | |
instanceInjector.strictDi = strictDi; | ||
forEach(runBlocks, function(fn) { if (fn) instanceInjector.invoke(fn); }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth putting this in a method to avoid duplicating it a few lines down? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the original PR the instanceInjector = protoInstanceInjector.get('$injector'); Are you suggesting that we just make a 'executeRunBlocks` method: function executeRunBlocks(blocks) {
forEach(blocks, function(fn) { if (fn) instanceInjector.invoke(fn); });
} which we then reuse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem to save much and there is very little logic involved so I am inclined not to bother. |
||
|
||
instanceInjector.loadNewModules = function(mods) { | ||
forEach(loadModules(mods), function(fn) { if (fn) instanceInjector.invoke(fn); }); | ||
}; | ||
|
||
|
||
return instanceInjector; | ||
|
||
//////////////////////////////////// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, the ability to pass functions/arrays as modules (e.g. in
angular.module()
) is undocumented. I think it makes sense to be consistent betweenangular.module()
andloadNewModules()
.(Either documenting or undocumenting in both places is fine by me.)
Also, nit: In most places we use
Array.<...>
(notice the dot afterArray
) 😇There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cut and pasted this from the
angular.bootstrap()
API code: https://github.com/angular/angular.js/blob/master/src/Angular.js#L1773There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😞 - I am not going to block the PR for that then 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then why does it say
mods
instead ofmodules
? 😛