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

Commit cf92c33

Browse files
committed
docs(*): clarify module API and dependency injection rules
Closes #16363 Closes #16395
1 parent 5d8f5a8 commit cf92c33

File tree

4 files changed

+77
-64
lines changed

4 files changed

+77
-64
lines changed

docs/content/guide/di.ngdoc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@ and providing them to other components as requested.
1414

1515
## Using Dependency Injection
1616

17-
DI is pervasive throughout Angular. You can use it when defining components or when providing `run`
18-
and `config` blocks for a module.
17+
Dependency Injection is pervasive throughout AngularJS. You can use it when defining components
18+
or when providing `run` and `config` blocks for a module.
1919

20-
- Components such as services, directives, filters, and animations are defined by an injectable
21-
factory method or constructor function. These components can be injected with "service" and "value"
22-
components as dependencies.
20+
- {@link angular.Module#service Services}, {@link angular.Module#directive directives},
21+
{@link angular.Module#filter filters}, and {@link angular.Module#animation animations} are
22+
defined by an injectable factory method or constructor function, and can be injected with
23+
"services", "values", and "constants" as dependencies.
2324

24-
- Controllers are defined by a constructor function, which can be injected with any of the "service"
25-
and "value" components as dependencies, but they can also be provided with special dependencies. See
26-
{@link di#controllers Controllers} below for a list of these special dependencies.
25+
- {@link ng.$controller Controllers} are defined by a constructor function, which can be injected
26+
with any of the "service" and "value" as dependencies, but they can also be provided with
27+
"special dependencies". See {@link di#controllers Controllers} below for a list of these
28+
special dependencies.
2729

28-
- The `run` method accepts a function, which can be injected with "service", "value" and "constant"
29-
components as dependencies. Note that you cannot inject "providers" into `run` blocks.
30+
- The {@link angular.Module#run `run`} method accepts a function, which can be injected with
31+
"services", "values" and, "constants" as dependencies. Note that you cannot inject "providers"
32+
into `run` blocks.
3033

31-
- The `config` method accepts a function, which can be injected with "provider" and "constant"
32-
components as dependencies. Note that you cannot inject "service" or "value" components into
33-
configuration.
34+
- The {@link angular.Module#config `config`} method accepts a function, which can be injected with
35+
"providers" and "constants" as dependencies. Note that you cannot inject "services" or
36+
"values" into configuration.
37+
38+
- The {@link angular.Module#provider `provider`} method can only be injected with other "providers".
39+
However, only those that have been **registered beforehand** can be injected. This is different
40+
from services, where the order of registration does not matter.
3441

3542
See {@link module#module-loading-dependencies Modules} for more details about `run` and `config`
36-
blocks.
43+
blocks and {@link guide/providers Providers} for more information about the different provider
44+
types.
3745

3846

3947
### Factory Methods

docs/content/guide/module.ngdoc

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
@sortOrder 320
44
@description
55

6-
# What is a Module?
6+
# Modules
7+
8+
## What is a Module?
79

810
You can think of a module as a container for the different parts of your app – controllers,
911
services, filters, directives, etc.
1012

11-
# Why?
13+
## Why?
1214

1315
Most applications have a main method that instantiates and wires together the different parts of
1416
the application.
@@ -23,7 +25,7 @@ should be bootstrapped. There are several advantages to this approach:
2325
* End-to-end tests can use modules to override configuration.
2426

2527

26-
# The Basics
28+
## The Basics
2729

2830
I'm in a hurry. How do I get a Hello World module working?
2931

@@ -65,7 +67,7 @@ Important things to notice:
6567
This array is the list of modules `myApp` depends on.
6668

6769

68-
# Recommended Setup
70+
## Recommended Setup
6971

7072
While the example above is simple, it will not scale to large applications. Instead we recommend
7173
that you break your application to multiple modules like this:
@@ -136,39 +138,46 @@ The above is a suggestion. Tailor it to your needs.
136138

137139

138140

139-
# Module Loading & Dependencies
141+
## Module Loading
140142

141-
A module is a collection of configuration and run blocks which get applied to the application
142-
during the bootstrap process. In its simplest form the module consists of a collection of two kinds
143-
of blocks:
143+
A {@link angular.Module module} is a collection of providers, services, directives etc.,
144+
and optionally config and run blocks which get applied to the application during the
145+
bootstrap process.
144146

145-
1. **Configuration blocks** - get executed during the provider registrations and configuration
146-
phase. Only providers and constants can be injected into configuration blocks. This is to
147-
prevent accidental instantiation of services before they have been fully configured.
148-
2. **Run blocks** - get executed after the injector is created and are used to kickstart the
149-
application. Only instances and constants can be injected into run blocks. This is to prevent
150-
further system configuration during application run time.
147+
The {@link angular.Module module API} describes all the available methods and how they can be used.
151148

152-
```js
153-
angular.module('myModule', []).
154-
config(function(injectables) { // provider-injector
155-
// This is an example of config block.
156-
// You can have as many of these as you want.
157-
// You can only inject Providers (not instances)
158-
// into config blocks.
159-
}).
160-
run(function(injectables) { // instance-injector
161-
// This is an example of a run block.
162-
// You can have as many of these as you want.
163-
// You can only inject instances (not Providers)
164-
// into run blocks
165-
});
166-
```
149+
See {@link guide/di#using-dependency-injection Using Dependency Injection} to find out which
150+
dependencies can be injected in each method.
151+
152+
### Dependencies and Order of execution
153+
154+
Modules can list other modules as their dependencies. Depending on a module implies that the required
155+
module will be loaded before the requiring module is loaded.
156+
157+
In a single module the order of execution is as follows:
158+
159+
1. {@link angular.Module#provider provider} functions are executed, so they and the services they
160+
define can be made available to the {@link auto.$injector $injector}.
167161

168-
## Configuration Blocks
162+
2. After that, the configuration blocks ({@link angular.Module#config config} functions) are executed.
163+
This means the configuration blocks of the required modules execute before the configuration blocks
164+
of any requiring module.
165+
166+
This continues until all module dependencies has been resolved.
167+
168+
Then, the {@link angular.Module#run run} blocks that have been collected from each module are
169+
executed in order of requirement.
170+
171+
Note: each module is only loaded once, even if multiple other modules require it.
172+
Note: the factory function for "values" and "services" is called lazily when the value/service is
173+
injected for the first time.
174+
175+
### Registration in the config block
176+
177+
While it is recommended to register injectables directly with the {@link angular.Module module API},
178+
it is also possible to register services, directives etc. by injecting
179+
{@link $provide $provide} or the individual service providers into the config function:
169180

170-
There are some convenience methods on the module which are equivalent to the `config` block. For
171-
example:
172181

173182
```js
174183
angular.module('myModule', []).
@@ -188,35 +197,22 @@ angular.module('myModule', []).
188197
});
189198
```
190199

191-
<div class="alert alert-info">
192-
When bootstrapping, first Angular applies all constant definitions.
193-
Then Angular applies configuration blocks in the same order they were registered.
194-
</div>
195-
196-
## Run Blocks
200+
### Run Blocks
197201

198202
Run blocks are the closest thing in Angular to the main method. A run block is the code which
199203
needs to run to kickstart the application. It is executed after all of the services have been
200204
configured and the injector has been created. Run blocks typically contain code which is hard
201205
to unit-test, and for this reason should be declared in isolated modules, so that they can be
202206
ignored in the unit-tests.
203207

204-
## Dependencies
205-
206-
Modules can list other modules as their dependencies. Depending on a module implies that the required
207-
module needs to be loaded before the requiring module is loaded. In other words the configuration
208-
blocks of the required modules execute before the configuration blocks of the requiring module.
209-
The same is true for the run blocks. Each module can only be loaded once, even if multiple other
210-
modules require it.
211-
212-
## Asynchronous Loading
208+
### Asynchronous Loading
213209

214210
Modules are a way of managing $injector configuration, and have nothing to do with loading of
215211
scripts into a VM. There are existing projects which deal with script loading, which may be used
216212
with Angular. Because modules do nothing at load time they can be loaded into the VM in any order
217213
and thus script loaders can take advantage of this property and parallelize the loading process.
218214

219-
## Creation versus Retrieval
215+
### Creation versus Retrieval
220216

221217
Beware that using `angular.module('myModule', [])` will create the module `myModule` and overwrite any
222218
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
@@ -235,7 +231,7 @@ var myModule = angular.module('myModule', []);
235231
var myModule = angular.module('myOtherModule');
236232
```
237233

238-
# Unit Testing
234+
## Unit Testing
239235

240236
A unit test is a way of instantiating a subset of an application to apply stimulus to it.
241237
Small, structured modules help keep unit tests concise and focused.

src/auto/injector.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ function annotate(fn, strictDi, name) {
440440
* which lets you specify whether the {@link ng.$log $log} service will log debug messages to the
441441
* console or not.
442442
*
443+
* It is possible to inject other providers into the provider function,
444+
* but the injected provider must have been defined before the one that requires it.
445+
*
443446
* @param {string} name The name of the instance. NOTE: the provider will be available under `name +
444447
'Provider'` key.
445448
* @param {(Object|function())} provider If the provider is:

src/loader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,13 @@ function setupModuleLoader(window) {
344344
* @param {Function} configFn Execute this function on module load. Useful for service
345345
* configuration.
346346
* @description
347-
* Use this method to register work which needs to be performed on module loading.
347+
* Use this method to configure services by injecting their
348+
* {@link angular.Module#provider `providers`}, e.g. for adding routes to the
349+
* {@link ngRoute.$routeProvider $routeProvider}.
350+
*
351+
* Note that you can only inject {@link angular.Module#provider `providers`} and
352+
* {@link angular.Module#constant `constants`} into this function.
353+
*
348354
* For more about how to configure services, see
349355
* {@link providers#provider-recipe Provider Recipe}.
350356
*/

0 commit comments

Comments
 (0)