@@ -11,7 +11,8 @@ multiple views by adding routing, using an Angular module called 'ngRoute'.
11
11
12
12
* When you now navigate to `app/index.html`, you are redirected to `app/index.html#/phones`
13
13
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.
15
16
16
17
<div doc-tutorial-reset="7"></div>
17
18
@@ -132,11 +133,11 @@ __`app/index.html`:__
132
133
</html>
133
134
```
134
135
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
136
137
application:
137
138
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 .
140
141
141
142
Note that we removed most of the code in the `index.html` template and replaced it with a single
142
143
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
191
192
192
193
## The App Module
193
194
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
+
194
204
__`app/js/app.js`:__
195
205
196
206
```js
@@ -199,6 +209,16 @@ var phonecatApp = angular.module('phonecatApp', [
199
209
'phonecatControllers'
200
210
]);
201
211
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
+
202
222
phonecatApp.config(['$routeProvider',
203
223
function($routeProvider) {
204
224
$routeProvider.
@@ -216,31 +236,28 @@ phonecatApp.config(['$routeProvider',
216
236
}]);
217
237
```
218
238
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.
229
242
230
243
Our application routes are defined as follows:
231
244
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.
234
256
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.
238
257
239
258
We reused the `PhoneListCtrl` controller that we constructed in previous steps and we added a new,
240
259
empty `PhoneDetailCtrl` controller to the `app/js/controllers.js` file for the phone details view.
241
260
242
- `$routeProvider.otherwise({redirectTo: '/phones'})` triggers a redirection to `/phones` when the browser
243
- address doesn't match either of our routes.
244
261
245
262
Note the use of the `:phoneId` parameter in the second route declaration. The `$route` service uses
246
263
the route declaration — `'/phones/:phoneId'` — as a template that is matched against the current
@@ -270,12 +287,14 @@ phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams',
270
287
}]);
271
288
```
272
289
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.
278
295
296
+ Because our example app is relatively small, we'll just add all of our controllers to the
297
+ `phonecatControllers` module.
279
298
280
299
281
300
## Test
0 commit comments