3
3
@sortOrder 320
4
4
@description
5
5
6
- # What is a Module?
6
+ # Modules
7
+
8
+ ## What is a Module?
7
9
8
10
You can think of a module as a container for the different parts of your app – controllers,
9
11
services, filters, directives, etc.
10
12
11
- # Why?
13
+ ## Why?
12
14
13
15
Most applications have a main method that instantiates and wires together the different parts of
14
16
the application.
@@ -23,7 +25,7 @@ should be bootstrapped. There are several advantages to this approach:
23
25
* End-to-end tests can use modules to override configuration.
24
26
25
27
26
- # The Basics
28
+ ## The Basics
27
29
28
30
I'm in a hurry. How do I get a Hello World module working?
29
31
@@ -65,7 +67,7 @@ Important things to notice:
65
67
This array is the list of modules `myApp` depends on.
66
68
67
69
68
- # Recommended Setup
70
+ ## Recommended Setup
69
71
70
72
While the example above is simple, it will not scale to large applications. Instead we recommend
71
73
that you break your application to multiple modules like this:
@@ -136,39 +138,46 @@ The above is a suggestion. Tailor it to your needs.
136
138
137
139
138
140
139
- # Module Loading & Dependencies
141
+ ## Module Loading
140
142
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.
144
146
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.
151
148
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}.
167
161
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:
169
180
170
- There are some convenience methods on the module which are equivalent to the `config` block. For
171
- example:
172
181
173
182
```js
174
183
angular.module('myModule', []).
@@ -188,35 +197,22 @@ angular.module('myModule', []).
188
197
});
189
198
```
190
199
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
197
201
198
202
Run blocks are the closest thing in Angular to the main method. A run block is the code which
199
203
needs to run to kickstart the application. It is executed after all of the services have been
200
204
configured and the injector has been created. Run blocks typically contain code which is hard
201
205
to unit-test, and for this reason should be declared in isolated modules, so that they can be
202
206
ignored in the unit-tests.
203
207
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
213
209
214
210
Modules are a way of managing $injector configuration, and have nothing to do with loading of
215
211
scripts into a VM. There are existing projects which deal with script loading, which may be used
216
212
with Angular. Because modules do nothing at load time they can be loaded into the VM in any order
217
213
and thus script loaders can take advantage of this property and parallelize the loading process.
218
214
219
- ## Creation versus Retrieval
215
+ ### Creation versus Retrieval
220
216
221
217
Beware that using `angular.module('myModule', [])` will create the module `myModule` and overwrite any
222
218
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
@@ -235,7 +231,7 @@ var myModule = angular.module('myModule', []);
235
231
var myModule = angular.module('myOtherModule');
236
232
```
237
233
238
- # Unit Testing
234
+ ## Unit Testing
239
235
240
236
A unit test is a way of instantiating a subset of an application to apply stimulus to it.
241
237
Small, structured modules help keep unit tests concise and focused.
0 commit comments