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

Commit f7cf680

Browse files
docs(tutorial/step-7): clarify the new files & modules
Closes #6996
1 parent e101c12 commit f7cf680

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

docs/content/tutorial/step_07.ngdoc

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ multiple views by adding routing, using an Angular module called 'ngRoute'.
1111

1212
* When you now navigate to `app/index.html`, you are redirected to `app/index.html#/phones`
1313
and the phone list appears in the browser.
14-
* When you click on a phone link the stub of a phone detail page is displayed.
14+
* When you click on a phone link the url changes to one specific to that phone and the stub of a
15+
phone detail page is displayed.
1516

1617
<div doc-tutorial-reset="7"></div>
1718

@@ -132,11 +133,11 @@ __`app/index.html`:__
132133
</html>
133134
```
134135

135-
We have added to extra `<script>` tags in our index file to load up extra JavaScript files into our
136+
We have added two new `<script>` tags in our index file to load up extra JavaScript files into our
136137
application:
137138

138-
- `angular-route.js` : defines the `ngRoute` module.
139-
- `controllers.js` : defines a new `phonecatControllers` module.
139+
- `angular-route.js` : defines the Angular `ngRoute` module, which provides us with routing.
140+
- `app.js` : this file now holds the root module of our application.
140141

141142
Note that we removed most of the code in the `index.html` template and replaced it with a single
142143
line containing a div with the `ng-view` attribute. The code that we removed was placed into the
@@ -191,6 +192,15 @@ Note how we are using the `phoneId` expression which will be defined in the `Pho
191192

192193
## The App Module
193194

195+
To improve the organization of the app, we are making use of Angular's `ngRoute` module and we've
196+
moved the controllers into their own module `phonecatControllers` (as shown below).
197+
198+
We added `angular-route.js` to `index.html` and created a new `phonecatControllers` module in
199+
`controllers.js`. That's not all we need to do to be able to use their code, however. We also have
200+
to add the modules dependencies of our app. By listing these two modules as dependencies of
201+
`phonecatApp`, we can use the directives and services they provide.
202+
203+
194204
__`app/js/app.js`:__
195205

196206
```js
@@ -199,6 +209,16 @@ var phonecatApp = angular.module('phonecatApp', [
199209
'phonecatControllers'
200210
]);
201211

212+
...
213+
```
214+
215+
Notice the second argument passed to `angular.module`, `['ngRoute', 'phonecatControllers']`. This
216+
array lists the modules that `phonecatApp` depends on.
217+
218+
219+
```js
220+
...
221+
202222
phonecatApp.config(['$routeProvider',
203223
function($routeProvider) {
204224
$routeProvider.
@@ -216,31 +236,28 @@ phonecatApp.config(['$routeProvider',
216236
}]);
217237
```
218238

219-
Above, we added `angular-route.js` and `controllers.js` to `index.html`. That's not all we need to
220-
do to be able to use their code, however. We also have to add the modules dependencies of our app.
221-
To improve the organization of the app, we've moved the controllers into their own file defining its
222-
own module `phonecatControllers` (as shown below). By listing these two modules as dependencies of
223-
`phonecatApp`, we can use the directives and services they provide. Notice the second argument
224-
passed to `angular.module`, `['ngRoute', 'phonecatControllers']`. This array lists the modules that
225-
`phonecatApp` depends on.
226-
227-
Using the `config` API we request the `$routeProvider` to be injected into our config function
228-
and use the {@link ngRoute.$routeProvider#when `$routeProvider.when`} API to define our routes.
239+
Using the `phonecatApp.config()` method, we request the `$routeProvider` to be injected into our
240+
config function and use the {@link ngRoute.$routeProvider#when `$routeProvider.when()`} method to
241+
define our routes.
229242

230243
Our application routes are defined as follows:
231244

232-
* The phone list view will be shown when the URL hash fragment is `/phones`. To construct this
233-
view, Angular will use the `phone-list.html` template and the `PhoneListCtrl` controller.
245+
* `when('/phones')`: The phone list view will be shown when the URL hash fragment is `/phones`. To
246+
construct this view, Angular will use the `phone-list.html` template and the `PhoneListCtrl`
247+
controller.
248+
249+
* `when('/phones/:phoneId')`: The phone details view will be shown when the URL hash fragment
250+
matches '/phone/:phoneId', where `:phoneId` is a variable part of the URL. To construct the phone
251+
details view, Angular will use the `phone-detail.html` template and the `PhoneDetailCtrl`
252+
controller.
253+
254+
* `otherwise({redirectTo: '/phones'})`: triggers a redirection to `/phones` when the browser
255+
address doesn't match either of our routes.
234256

235-
* The phone details view will be shown when the URL hash fragment matches '/phone/:phoneId', where
236-
`:phoneId` is a variable part of the URL. To construct the phone details view, Angular will use the
237-
`phone-detail.html` template and the `PhoneDetailCtrl` controller.
238257

239258
We reused the `PhoneListCtrl` controller that we constructed in previous steps and we added a new,
240259
empty `PhoneDetailCtrl` controller to the `app/js/controllers.js` file for the phone details view.
241260

242-
`$routeProvider.otherwise({redirectTo: '/phones'})` triggers a redirection to `/phones` when the browser
243-
address doesn't match either of our routes.
244261

245262
Note the use of the `:phoneId` parameter in the second route declaration. The `$route` service uses
246263
the route declaration — `'/phones/:phoneId'` — as a template that is matched against the current
@@ -270,12 +287,14 @@ phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams',
270287
}]);
271288
```
272289

273-
Again, note that we created a new module called `phonecatControllers`. For small AngularJS applications,
274-
it's common to create just one module for all of your controllers if there are just a few. For larger apps,
275-
you will probably want to create separate modules for each major feature of your app.
276-
277-
Because our example app is relatively small, we'll add all of our controllers to this module.
290+
Again, note that we created a new module called `phonecatControllers`. For small AngularJS
291+
applications, it's common to create just one module for all of your controllers if there are just a
292+
few. As your application grows it is quite common to refactor your code into additional modules.
293+
For larger apps, you will probably want to create separate modules for each major feature of
294+
your app.
278295

296+
Because our example app is relatively small, we'll just add all of our controllers to the
297+
`phonecatControllers` module.
279298

280299

281300
## Test

0 commit comments

Comments
 (0)