@@ -46,7 +46,7 @@ history (back and forward navigation) and bookmarks.
46
46
47
47
### A Note About DI, Injector and Providers
48
48
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
50
50
AngularJS, so it's important for you to understand a thing or two about how it works.
51
51
52
52
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.
69
69
70
70
__`app/js/app.js`:__
71
71
<pre>
72
- var phonecatApp = angular.module('phonecatApp', ['phonecatFilters']);
72
+ var phonecatApp = angular.module('phonecatApp', [
73
+ 'ngRoute',
74
+ 'phonecatControllers'
75
+ ]);
73
76
74
77
phonecatApp.config(['$routeProvider',
75
78
function($routeProvider) {
@@ -89,14 +92,19 @@ phonecatApp.config(['$routeProvider',
89
92
</pre>
90
93
91
94
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 .
94
97
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.
98
103
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:
100
108
101
109
* The phone list view will be shown when the URL hash fragment is `/phones`. To construct this
102
110
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
108
116
We reused the `PhoneListCtrl` controller that we constructed in previous steps and we added a new,
109
117
empty `PhoneDetailCtrl` controller to the `app/js/controllers.js` file for the phone details view.
110
118
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.
113
121
114
122
Note the use of the `:phoneId` parameter in the second route declaration. The `$route` service uses
115
123
the route declaration — `'/phones/:phoneId'` — as a template that is matched against the current
116
124
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.
118
126
119
127
120
128
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`:__
133
141
134
142
__`app/js/controllers.js`:__
135
143
<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',
138
156
function($scope, $routeParams) {
139
157
$scope.phoneId = $routeParams.phoneId;
140
158
}]);
141
159
</pre>
142
160
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.
143
166
144
167
## Template
145
168
146
169
The `$route` service is usually used in conjunction with the {@link api/ngRoute.directive:ngView
147
170
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>
149
178
150
179
__`app/index.html`:__
151
180
<pre>
152
181
<html lang="en" ng-app="phonecatApp">
153
182
<head>
154
183
...
155
184
<script src="lib/angular/angular.js"></script>
185
+ <script src="lib/angular/angular-route.js"></script>
156
186
<script src="js/app.js"></script>
157
187
<script src="js/controllers.js"></script>
158
188
</head>
0 commit comments