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

Commit c925775

Browse files
committed
docs(*): clarify module API and dependency injection rules
Closes #16363
1 parent 4ff51bf commit c925775

File tree

4 files changed

+77
-67
lines changed

4 files changed

+77
-67
lines changed

docs/content/guide/di.ngdoc

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@ and providing them to other components as requested.
1414

1515
## Using Dependency Injection
1616

17-
DI is pervasive throughout AngularJS. You can use it when defining components or when providing `run`
18-
and `config` blocks for a module.
19-
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.
23-
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.
27-
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-
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.
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.
19+
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+
"service" and "value" as dependencies.
24+
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.
29+
30+
- The {@link angular.Module#run `run`} method accepts a function, which can be injected with
31+
"service", "value" and "constant" as dependencies. Note that you cannot inject "providers"
32+
into `run` blocks.
33+
34+
- The {@link angular.Module#config `config`} method accepts a function, which can be injected with
35+
"provider" and "constant" as dependencies. Note that you cannot inject "service" or
36+
"value" components into configuration.
37+
38+
- The {@link angular.Module#provider `provider`} method can only be injected with other "provider"
39+
injectables, and only with those that have been **registered beforehand**. 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`
3643
blocks.

docs/content/guide/module.ngdoc

Lines changed: 43 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,44 @@ 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 configuration 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 which injectables.
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.
169165

170-
There are some convenience methods on the module which are equivalent to the `config` block. For
171-
example:
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 that each module is only loaded once, even if multiple other modules require it.
172+
173+
174+
### Registering injectables in the config block
175+
176+
While it is recommended to register injectables directly with the {@link angular.Module module API},
177+
it is also possible to register services, directives etc. by injecting into the config function
178+
{@link $provide $provide} or the individual service providers:
172179

173180
```js
174181
angular.module('myModule', []).
@@ -188,35 +195,22 @@ angular.module('myModule', []).
188195
});
189196
```
190197

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

198200
Run blocks are the closest thing in AngularJS to the main method. A run block is the code which
199201
needs to run to kickstart the application. It is executed after all of the services have been
200202
configured and the injector has been created. Run blocks typically contain code which is hard
201203
to unit-test, and for this reason should be declared in isolated modules, so that they can be
202204
ignored in the unit-tests.
203205

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
206+
### Asynchronous Loading
213207

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

219-
## Creation versus Retrieval
213+
### Creation versus Retrieval
220214

221215
Beware that using `angular.module('myModule', [])` will create the module `myModule` and overwrite any
222216
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
@@ -235,7 +229,7 @@ var myModule = angular.module('myModule', []);
235229
var myModule = angular.module('myOtherModule');
236230
```
237231

238-
# Unit Testing
232+
## Unit Testing
239233

240234
A unit test is a way of instantiating a subset of an application to apply stimulus to it.
241235
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)