Skip to content
This repository was archived by the owner on Mar 1, 2025. It is now read-only.

Commit d07101d

Browse files
committed
docs(guide/module): improve clarity
1 parent 2206b99 commit d07101d

File tree

1 file changed

+71
-64
lines changed

1 file changed

+71
-64
lines changed

docs/content/guide/module.ngdoc

+71-64
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55
# What is a Module?
66

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+
815
Angular apps don't have a main method. Instead modules declaratively specify how an application
916
should be bootstrapped. There are several advantages to this approach:
1017

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.
1723

1824

1925
# The Basics
2026

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?
2828

2929
<example module='myApp'>
3030
<file name="index.html">
@@ -47,25 +47,29 @@ Important things to notice:
4747
</file>
4848
</example>
4949

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.
5057

5158

5259
# Recommended Setup
5360

5461
While the example above is simple, it will not scale to large applications. Instead we recommend
5562
that you break your application to multiple modules like this:
5663

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
6167
initialization code.
6268

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.
6771

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.
6973

7074
<example module='xmpl'>
7175
<file name="index.html">
@@ -133,19 +137,19 @@ angular.module('myModule', []).
133137
// This is an example of config block.
134138
// You can have as many of these as you want.
135139
// You can only inject Providers (not instances)
136-
// into the config blocks.
140+
// into config blocks.
137141
}).
138142
run(function(injectables) { // instance-injector
139143
// This is an example of a run block.
140144
// You can have as many of these as you want.
141145
// You can only inject instances (not Providers)
142-
// into the run blocks
146+
// into run blocks
143147
});
144148
```
145149

146150
## Configuration Blocks
147151

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
149153
example:
150154

151155
```js
@@ -166,8 +170,10 @@ angular.module('myModule', []).
166170
});
167171
```
168172

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>
171177

172178
## Run Blocks
173179

@@ -198,72 +204,73 @@ Beware that using `angular.module('myModule', [])` will create the module `myMod
198204
existing module named `myModule`. Use `angular.module('myModule')` to retrieve an existing module.
199205

200206
```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', ...);
206212

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', []);
209215

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');
212218
```
213219

214220
# Unit Testing
215221

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>
221230

222231
In all of these examples we are going to assume this module definition:
223232

224233
```js
225-
angular.module('greetMod', []).
234+
angular.module('greetMod', []).
226235

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+
}).
232241

233-
value('salutation', 'Hello').
242+
value('salutation', 'Hello').
234243

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+
});
240249
```
241250

242-
Let's write some tests:
251+
Let's write some tests to show how to override configuration in tests.
243252

244253
```js
245254
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.
251259
beforeEach(module('greetMod', function($provide) {
252260
$provide.value('$window', {
253261
alert: jasmine.createSpy('alert')
254262
});
255263
}));
256264

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.
260267
it('should alert on $window', inject(function(greet, $window) {
261268
greet('World');
262269
expect($window.alert).toHaveBeenCalledWith('Hello World!');
263270
}));
264271

265272
// 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.
267274
it('should alert using the alert service', function() {
268275
var alertSpy = jasmine.createSpy('alert');
269276
module(function($provide) {

0 commit comments

Comments
 (0)