8000 docs(tutorial/step07): improve explanations, add note about ngRoute · olostan/angular.js@01cbe2f · GitHub
[go: up one dir, main page]

Skip to content

Commit 01cbe2f

Browse files
committed
docs(tutorial/step07): improve explanations, add note about ngRoute
1 parent 08ba913 commit 01cbe2f

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

docs/content/tutorial/step_07.ngdoc

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ history (back and forward navigation) and bookmarks.
4646

4747
### A Note About DI, Injector and Providers
4848

49-
As you {@link tutorial/step_05 noticed}, {@link guide/di dependency injection} (DI) is the core feature of
49+
As you {@link tutorial/step_05 noticed}, {@link guide/di dependency injection} (DI) is at the core of
5050
AngularJS, so it's important for you to understand a thing or two about how it works.
5151

5252
When the application bootstraps, Angular creates an injector that will be used for all DI stuff in
@@ -69,7 +69,10 @@ both module systems can live side by side and fulfil their goals.
6969

7070
__`app/js/app.js`:__
7171
<pre>
72-
var phonecatApp = angular.module('phonecatApp', ['phonecatFilters']);
72+
var phonecatApp = angular.module('phonecatApp', [
73+
'ngRoute',
74+
'phonecatControllers'
75+
]);
7376

7477
phonecatApp.config(['$routeProvider',
7578
function($routeProvider) {
@@ -89,14 +92,19 @@ phonecatApp.config(['$routeProvider',
8992
</pre>
9093

9194
In order to configure our application with routes, we need to create a module for our application.
92-
We call this module `phonecat` and using the `config` API we request the `$routeProvider` to be
93-
injected into our config function and use `$routeProvider.when` API to define our routes.
95+
We call this module `phonecatApp`. Notice the second argument passed to `angular.module`:
96+
`['ngRoute', 'phonecatControllers']`. This array lists the modules that `phonecatApp` depends on.
9497

95-
Note that during the injector configuration phase, the providers can be injected as well, but they
96-
will not be available for injection once the injector is created and starts creating service
97-
instances.
98+
Above, we added `angular-route.js` to `index.html`. That's not all we need to do to be able to use
99+
it, however. We also have to add `ngRoute` as a dependency of our app. To improve the organization
100+
of the app, we're going to move the controllers into their own file (as shown below), and call the
101+
module `phonecatControllers`. By listing these two modules as dependencies of `phonecatApp`, we
102+
can use the directives and services they provide.
98103

99-
Our application routes were defined as follows:
104+
Thus using the `config` API we request the `$routeProvider` to be injected into our config function
105+
and use the {@link api/ngRoute.$routeProvider#when `$routeProvider.when`} API to define our routes.
106+
107+
Our application routes are defined as follows:
100108

101109
* The phone list view will be shown when the URL hash fragment is `/phones`. To construct this
102110
view, Angular will use the `phone-list.html` template and the `PhoneListCtrl` controller.
@@ -108,13 +116,13 @@ view, Angular will use the `phone-list.html` template and the `PhoneListCtrl` co
108116
We reused the `PhoneListCtrl` controller that we constructed in previous steps and we added a new,
109117
empty `PhoneDetailCtrl` controller to the `app/js/controllers.js` file for the phone details view.
110118

111-
The statement `$route.otherwise({redirectTo: '/phones'})` triggers a redirection to `/phones` when
112-
the browser address doesn't match either of our routes.
119+
`$route.otherwise({redirectTo: '/phones'})` triggers a redirection to `/phones` when the browser
120+
address doesn't match either of our routes.
113121

114122
Note the use of the `:phoneId` parameter in the second route declaration. The `$route` service uses
115123
the route declaration — `'/phones/:phoneId'` — as a template that is matched against the current
116124
URL. All variables defined with the `:` notation are extracted into the
117-
{@link api/ngRoute.$routeParams $routeParams} object.
125+
{@link api/ngRoute.$routeParams `$routeParams`} object.
118126

119127

120128
In order for our application to bootstrap with our newly created module we'll also need to specify
@@ -133,26 +141,48 @@ __`app/index.html`:__
133141

134142
__`app/js/controllers.js`:__
135143
<pre>
136-
...
137-
phonecatApp.controller('PhoneDetailCtrl', ['$scope', '$routeParams',
144+
var phonecatControllers = angular.module('phonecatControllers', []);
145+
146+
phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http',
147+
function PhoneListCtrl($scope, $http) {
148+
$http.get('phones/phones.json').success(function(data) {
149+
$scope.phones = data;
150+
});
151+
152+
$scope.orderProp = 'age';
153+
}]);
154+
155+
phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams',
138156
function($scope, $routeParams) {
139157
$scope.phoneId = $routeParams.phoneId;
140158
}]);
141159
</pre>
142160

161+
Again, note that we created a new module called `phonecatControllers`. For small AngularJS applications,
162+
it's common to create just one module for all of your controllers if there are just a few. For larger apps,
163+
you will rpobbaly want to create separate modules for each major feature of your app.
164+
165+
Because our example app is relatively small, we'll add all of our controllers to this module.
143166

144167
## Template
145168

146169
The `$route` service is usually used in conjunction with the {@link api/ngRoute.directive:ngView
147170
ngView} directive. The role of the `ngView` directive is to include the view template for the current
148-
route into the layout template, which makes it a perfect fit for our `index.html` template.
171+
route into the layout template. This makes it a perfect fit for our `index.html` template.
172+
173+
<div class="alert alert-info">
174+
**Note:** Starting with AngularJS version 1.2, `ngRoute` is in its own module and must be loaded by loading
175+
the `angular-route.js` file distributed with Angular. The easist way to load the file is to add a `<scipt>`
176+
tag to your `index.html` file as shown below.
177+
</div>
149178

150179
__`app/index.html`:__
151180
<pre>
152181
<html lang="en" ng-app="phonecatApp">
153182
<head>
154183
...
155184
<script src="lib/angular/angular.js"></script>
185+
<script src="lib/angular/angular-route.js"></script>
156186
<script src="js/app.js"></script>
157187
<script src="js/controllers.js"></script>
158188
</head>

0 commit comments

Comments
 (0)
0