Improve this doc

Enough of building an app with three phones in a hard-coded dataset! Let's fetch a larger dataset from our server using one of angular's built-in services called $http. We will use angular's dependency injection (DI) to provide the service to the PhoneListCtrl controller.

You should now see a list of 20 phones.

The most important changes are listed below. You can see the full diff on GitHub:

Data

The app/phones/phones.json file in your project is a dataset that contains a larger list of phones stored in the JSON format.

Following is a sample of the file:

[
 {
  "age": 13,
  "id": "motorola-defy-with-motoblur",
  "name": "Motorola DEFY\u2122 with MOTOBLUR\u2122",
  "snippet": "Are you ready for everything life throws your way?"
  ...
 },
...
]

Controller

We'll use angular's $http service in our controller to make an HTTP request to your web server to fetch the data in the app/phones/phones.json file. $http is just one of several built-in angular services that handle common operations in web apps. Angular injects these services for you where you need them.

Services are managed by angular's DI subsystem. Dependency injection helps to make your web apps both well-structured (e.g., separate components for presentation, data, and control) and loosely coupled (dependencies between components are not resolved by the components themselves, but by the DI subsystem).

app/js/controllers.js:

function PhoneListCtrl($scope, $http) {
  $http.get('phones/phones.json').success(function(data) {
    $scope.phones = data;
  });

  $scope.orderProp = 'age';
}

//PhoneListCtrl.$inject = ['$scope', '$http'];

$http makes an HTTP GET request to our web server, asking for phone/phones.json (the url is relative to our index.html file). The server responds by providing the data in the json file. (The response might just as well have been dynamically generated by a backend server. To the browser and our app they both look the same. For the sake of simplicity we used a json file in this tutorial.)

The $http service returns a promise object with a success method. We call this method to handle the asynchronous response and assign the phone data to the scope controlled by this controller, as a model called phones. Notice that angular detected the json response and parsed it for us!

To use a service in angular, you simply declare the names of the dependencies you need as arguments to the controller's constructor function, as follows:

function PhoneListCtrl($scope, $http) {...}

Angular's dependency injector provides services to your controller when the controller is being constructed. The dependency injector also takes care of creating any transitive dependencies the service may have (services often depend upon other services).

Note that the names of arguments are significant, because the injector uses these to look up the dependencies.

'$' Prefix Naming Convention

You can create your own services, and in fact we will do exactly that in step 11. As a naming convention, angular's built-in services, Scope methods and a few other angular APIs have a '$' prefix in front of the name. Don't use a '$' prefix when naming your services and models, in order to avoid any possible naming collisions.

A Note on Minification

Since angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

To overcome issues caused by minification, just assign an array with service identifier strings into the $inject property of the controller function, just like the last line in the snippet (commented out) suggests:

PhoneListCtrl.$inject = ['$scope', '$http'];

There is also one more way to specify this dependency list and avoid minification issues — using the bracket notation which wraps the function to be injected into an array of strings (representing the dependency names) followed by the function to be injected:

var PhoneListCtrl = ['$scope', '$http', function($scope, $http) { /* constructor body */ }];

Both of these methods work with any function that can be injected by Angular, so it's up to your project's style guide to decide which one you use.

Test

test/unit/controllersSpec.js:

Because we started using dependency injection and our controller has dependencies, constructing the controller in our tests is a bit more complicated. We could use the new operator and provide the constructor with some kind of fake $http implementation. However, the recommended (and easier) way is to create a controller in the test environment in the same way that angular does it in the production code behind the scenes, as follows:

describe('PhoneCat controllers', function() {

  describe('PhoneListCtrl', function(){
    var scope, ctrl, $httpBackend;

    // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
    // This allows us to inject a service but then attach it to a variable
    // with the same name as the service.
    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET('phones/phones.json').
          respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);

      scope = $rootScope.$new();
      ctrl = $controller(PhoneListCtrl, {$scope: scope});
    }));

Note: Because we loaded Jasmine and angular-mocks.js in our test environment, we got two helper methods module and inject that we'll use to access and configure the injector.

We created the controller in the test environment, as follows:

Because our code now uses the $http service to fetch the phone list data in our controller, before we create the PhoneListCtrl child scope, we need to tell the testing harness to expect an incoming request from the controller. To do this we:

Now, we will make assertions to verify that the phones model doesn't exist on scope before the response is received:

    it('should create "phones" model with 2 phones fetched from xhr', function() {
      expect(scope.phones).toBeUndefined();
      $httpBackend.flush();

      expect(scope.phones).toEqual([{name: 'Nexus S'},
                                   {name: 'Motorola DROID'}]);
    });

Finally, we verify that the default value of orderProp is set correctly:

    it('should set the default value of orderProp model', function() {
      expect(scope.orderProp).toBe('age');
    });

You should now see the following output in the Karma tab:

   Chrome 22.0: Executed 2 of 2 SUCCESS (0.028 secs / 0.007 secs)

Experiments

Summary

Now that you have learned how easy it is to use angular services (thanks to Angular's dependency injection), go to step 6, where you will add some thumbnail images of phones and some links.