4
4
5
5
# What is a Module?
6
6
7
- Most applications have a main method which instantiates, wires, and bootstraps the application.
7
+ You can think of a module as a container for the different parts of your app – controllers,
8
+ services, filters, directives, etc.
9
+
10
+ # Why?
11
+
12
+ Most applications have a main method that instantiates and wires together the different parts of
13
+ the application.
14
+
8
15
Angular apps don't have a main method. Instead modules declaratively specify how an application
9
16
should be bootstrapped. There are several advantages to this approach:
10
17
11
- * The process is more declarative which is easier to understand
12
- * In unit-testing there is no need to load all modules, which may aid in writing unit-tests.
13
- * Additional modules can be loaded in scenario tests, which can override some of the
14
- configuration and help end-to-end test the application
15
- * Third party code can be packaged as reusable modules.
16
- * The modules can be loaded in any/parallel order (due to delayed nature of module execution).
18
+ * The declarative process is easier to understand.
19
+ * You can package code as reusable modules.
20
+ * The modules can be loaded in any order (or even in parallel) because modules delay execution.
21
+ * Unit tests only have to load relevant modules, which keeps them fast.
22
+ * End-to-end tests can use modules to override configuration.
17
23
18
24
19
25
# The Basics
20
26
21
- Ok, I'm in a hurry. How do I get a Hello World module working?
22
-
23
- Important things to notice:
24
-
25
- * {@link angular.Module Module} API
26
- * Notice the reference to the `myApp` module in the `<html ng-app="myApp">`, it is what
27
- bootstraps the app using your module.
27
+ I'm in a hurry. How do I get a Hello World module working?
28
28
29
29
<example module='myApp'>
30
30
<file name="index.html">
@@ -47,25 +47,29 @@ Important things to notice:
47
47
</file>
48
48
</example>
49
49
50
+ Important things to notice:
51
+
52
+ * The {@link angular.Module Module} API
53
+ * The reference to `myApp` module in `<html ng-app="myApp">`.
54
+ This is what bootstraps the app using your module.
55
+ * The empty array in `angular.module('myApp', [])`.
56
+ This array is the list of modules `myApp` depends on.
50
57
51
58
52
59
# Recommended Setup
53
60
54
61
While the example above is simple, it will not scale to large applications. Instead we recommend
55
62
that you break your application to multiple modules like this:
56
63
57
- * A service module, for service declaration
58
- * A directive module, for directive declaration
59
- * A filter module, for filter declaration
60
- * And an application level module which depends on the above modules, and which has
64
+ * A module for each feature
65
+ * A module for each reusable component (especially directives and filters)
66
+ * And an application level module which depends on the above modules and contains any
61
67
initialization code.
62
68
63
- The reason for this breakup is that in your tests, it is often necessary to ignore the
64
- initialization code, which tends to be difficult to test. By putting it into a separate module it
65
- can be easily ignored in tests. The tests can also be more focused by only loading the modules
66
- that are relevant to tests.
69
+ We've also written a document on how we organize large apps at Google and on how to write
70
+ reusable components.
67
71
68
- The above is only a suggestion, so feel free to tailor it to your needs.
72
+ The above is a suggestion. Tailor it to your needs.
69
73
70
74
<example module='xmpl'>
71
75
<file name="index.html">
@@ -133,19 +137,19 @@ angular.module('myModule', []).
133
137
// This is an example of config block.
134
138
// You can have as many of these as you want.
135
139
// You can only inject Providers (not instances)
136
- // into the config blocks.
140
+ // into config blocks.
137
141
}).
138
142
run(function(injectables) { // instance-injector
139
143
// This is an example of a run block.
140
144
// You can have as many of these as you want.
141
145
// You can only inject instances (not Providers)
142
- // into the run blocks
146
+ // into run blocks
143
147
});
144
148
```
145
149
146
150
## Configuration Blocks
147
151
148
- There are some convenience methods on the module which are equivalent to the config block. For
152
+ There are some convenience methods on the module which are equivalent to the ` config` block. For
149
153
example:
150
154
151
155
```js
@@ -166,8 +170,10 @@ angular.module('myModule', []).
166
170
});
167
171
```
168
172
169
- The configuration blocks get applied in the order in which they are registered. The only exception
170
- to it are constant definitions, which are placed at the beginning of all configuration blocks.
173
+ <div class="alert alert-info">
174
+ When bootstrapping, first Angular applies all constant definitions.
175
+ Then Angular applies configuration blocks in the order same order they were registered.
176
+ </div>
171
177
172
178
## Run Blocks
173
179
@@ -198,72 +204,73 @@ Beware that using `angular.module('myModule', [])` will create the module `myMod
198
204
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
199
205
200
206
```js
201
- var myModule = angular.module('myModule', []);
202
-
203
- // add some directives and services
204
- myModule.service('myService', ...);
205
- myModule.directive('myDirective', ...);
207
+ var myModule = angular.module('myModule', []);
208
+
209
+ // add some directives and services
210
+ myModule.service('myService', ...);
211
+ myModule.directive('myDirective', ...);
206
212
207
- // overwrites both myService and myDirective by creating a new module
208
- var myModule = angular.module('myModule', []);
213
+ // overwrites both myService and myDirective by creating a new module
214
+ var myModule = angular.module('myModule', []);
209
215
210
- // throws an error because myOtherModule has yet to be defined
211
- var myModule = angular.module('myOtherModule');
216
+ // throws an error because myOtherModule has yet to be defined
217
+ var myModule = angular.module('myOtherModule');
212
218
```
213
219
214
220
# Unit Testing
215
221
216
- In its simplest form a unit test is a way of instantiating a subset of the application in test and
217
- then applying a stimulus to it. It is important to realize that each module can only be loaded
218
- once per injector. Typically an app has only one injector. But in tests, each test has its own
219
- injector, which means that the modules are loaded multiple times per VM. Properly structured
220
- modules can help with unit testing, as in this example:
222
+ A unit test is a way of instantiating a subset of an application to apply stimulus to it.
223
+ Small, structured modules help keep unit tests concise and focused.
224
+
225
+ <div class="did you know...">
226
+ Each module can only be loaded once per injector.
227
+ Usually an Angular app has only one injector and modules are only loaded once.
228
+ Each test has its own injector and modules are loaded multiple times.
229
+ </div>
221
230
222
231
In all of these examples we are going to assume this module definition:
223
232
224
233
```js
225
- angular.module('greetMod', []).
234
+ angular.module('greetMod', []).
226
235
227
- factory('alert', function($window) {
228
- return function(text) {
229
- $window.alert(text);
230
- }
231
- }).
236
+ factory('alert', function($window) {
237
+ return function(text) {
238
+ $window.alert(text);
239
+ }
240
+ }).
232
241
233
- value('salutation', 'Hello').
242
+ value('salutation', 'Hello').
234
243
235
- factory('greet', function(alert, salutation) {
236
- return function(name) {
237
- alert(salutation + ' ' + name + '!');
238
- }
239
- });
244
+ factory('greet', function(alert, salutation) {
245
+ return function(name) {
246
+ alert(salutation + ' ' + name + '!');
247
+ }
248
+ });
240
249
```
241
250
242
- Let's write some tests:
251
+ Let's write some tests to show how to override configuration in tests.
243
252
244
253
```js
245
254
describe('myApp', function() {
246
- // load the relevant application modules then load a special
247
- // test module which overrides the $window with a mock version,
248
- // so that calling window.alert() will not block the test
249
- // runner with a real alert box. This is an example of overriding
250
- // configuration information in tests.
255
+ // load application module (`greetMod`) then load a special
256
+ // test module which overrides `$window` with a mock version,
257
+ // so that calling `window.alert()` will not block the test
258
+ // runner with a real alert box.
251
259
beforeEach(module('greetMod', function($provide) {
252
260
$provide.value('$window', {
253
261
alert: jasmine.createSpy('alert')
254
262
});
255
263
}));
256
264
257
- // The inject() will create the injector and inject the greet and
258
- // $window into the tests. The test need not concern itself with
259
- // wiring of the application, only with testing it.
265
+ // inject() will create the injector and inject the `greet` and
266
+ // `$window` into the tests.
260
267
it('should alert on $window', inject(function(greet, $window) {
261
268
greet('World');
262
269
expect($window.alert).toHaveBeenCalledWith('Hello World!');
263
270
}));
264
271
265
272
// this is another way of overriding configuration in the
266
- // tests using an inline module and inject methods.
273
+ // tests using inline ` module` and ` inject` methods.
267
274
it('should alert using the alert service', function() {
268
275
var alertSpy = jasmine.createSpy('alert');
269
276
module(function($provide) {
0 commit comments