6
6
7
7
8
8
Enough of building an app with three phones in a hard-coded dataset! Let's fetch a larger dataset
9
- from our server using one of angular 's built-in {@link api/ng services} called {@link
10
- api/ng.$http $http}. We will use angular 's {@link guide/di dependency
9
+ from our server using one of Angular 's built-in {@link api/ng services} called {@link
10
+ api/ng.$http $http}. We will use Angular 's {@link guide/di dependency
11
11
injection (DI)} to provide the service to the `PhoneListCtrl` controller.
12
12
13
13
@@ -42,12 +42,12 @@ Following is a sample of the file:
42
42
43
43
## Controller
44
44
45
- We'll use angular 's {@link api/ng.$http $http} service in our controller to make an HTTP
45
+ We'll use Angular 's {@link api/ng.$http $http} service in our controller to make an HTTP
46
46
request to your web server to fetch the data in the `app/phones/phones.json` file. `$http` is just
47
47
one of several built-in {@link guide/dev_guide.services angular services} that handle common operations
48
48
in web apps. Angular injects these services for you where you need them.
49
49
50
- Services are managed by angular 's {@link guide/di DI subsystem}. Dependency injection
50
+ Services are managed by Angular 's {@link guide/di DI subsystem}. Dependency injection
51
51
helps to make your web apps both well-structured (e.g., separate components for presentation, data,
52
52
and control) and loosely coupled (dependencies between components are not resolved by the
53
53
components themselves, but by the DI subsystem).
@@ -56,7 +56,7 @@ __`app/js/controllers.js:`__
56
56
<pre>
57
57
var phonecatApp = angular.module('phonecatApp', []);
58
58
59
- phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope) {
59
+ phonecatApp.controller('PhoneListCtrl', function PhoneListCtrl($scope, $http ) {
60
60
$http.get('phones/phones.json').success(function(data) {
61
61
$scope.phones = data;
62
62
});
@@ -92,16 +92,22 @@ dependencies.
92
92
<img class="diagram" src="img/tutorial/xhr_service_final.png">
93
93
94
94
95
- ### '$' Prefix Naming Convention
95
+ ### `$` Prefix Naming Convention
96
96
97
97
You can create your own services, and in fact we will do exactly that in step 11. As a naming
98
- convention, angular's built-in services, Scope methods and a few other angular APIs have a '$'
99
- prefix in front of the name. Don't use a '$' prefix when naming your services and models, in order
100
- to avoid any possible naming collisions.
98
+ convention, angular's built-in services, Scope methods and a few other Angular APIs have a `$`
99
+ prefix in front of the name.
100
+
101
+ The `$` prefix is there to namespace Angular-provided services.
102
+ It's best to avoid naming your services and models anything that begins with a `$` to avoid
103
+
104
+ If you inspect a Scope, you may also notice some properties that begin with `$$`. These
105
+ properties are considered private, and should not be accessed or modified.
106
+
101
107
102
108
### A Note on Minification
103
109
104
- Since angular infers the controller's dependencies from the names of arguments to the controller's
110
+ Since Angular infers the controller's dependencies from the names of arguments to the controller's
105
111
constructor function, if you were to {@link http://en.wikipedia.org/wiki/Minification_(programming)
106
112
minify} the JavaScript code for `PhoneListCtrl` controller, all of its function arguments would be
107
113
minified as well, and the dependency injector would not be able to identify services correctly.
@@ -131,6 +137,23 @@ anonymous function when registering the controller:
131
137
phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {...}]);
132
138
133
139
140
+ From this point onward, we're going to use the inline method in the tutorial. With that in mind,
141
+ let's add the annotations to our `PhoneListCtrl`:
142
+
143
+ __`app/js/controllers.js:`__
144
+ <pre>
145
+ var phonecatApp = angular.module('phonecatApp', []);
146
+
147
+ phonecatApp.controller('PhoneListCtrl', ['$scope', '$http',
148
+ function PhoneListCtrl($scope, $http) {
149
+ $http.get('phones/phones.json').success(function(data) {
150
+ $scope.phones = data;
151
+ });
152
+
153
+ $scope.orderProp = 'age';
154
+ }]);
155
+ </pre>
156
+
134
157
## Test
135
158
136
159
__`test/unit/controllersSpec.js`:__
@@ -192,7 +215,7 @@ native APIs and the global state associated with them — both of which make tes
192
215
HTTP request and tell it what to respond with. Note that the responses are not returned until we call
193
216
the `$httpBackend.flush` method.
194
217
195
- Now, we will make assertions to verify that the `phones` model doesn't exist on `scope` before
218
+ Now we will make assertions to verify that the `phones` model doesn't exist on `scope` before
196
219
the response is received:
197
220
198
221
<pre>
@@ -230,7 +253,7 @@ You should now see the following output in the Karma tab:
230
253
displayed in json format.
231
254
232
255
* In the `PhoneListCtrl` controller, pre-process the http response by limiting the number of phones
233
- to the first 5 in the list. Use the following code in the $http callback:
256
+ to the first 5 in the list. Use the following code in the ` $http` callback:
234
257
235
258
$scope.phones = data.splice(0, 5);
236
259
0 commit comments