[AngularJS]25. Built-in Angular Services

We can use this built in $http Service to make requests to a server(or in our case a json file). Give our StoreController access to the products using a service.

Pass our store controller the $http Service.

  app.controller('StoreController', ['$http', function($http){
    var store = this;
    store.products = [];
  }]);

 

get the store-products.json using the $http Service.

  app.controller('StoreController', ['$http', function($http){
    var store = this;
    store.products = [];
    $http.get('/store-products.json');
  }]);

 

Attach a success to our get call.

Name the first parameter of the success function data.

  app.controller('StoreController', ['$http', function($http){
    var store = this;
    store.products = [];
    $http.get('/store-products.json').success(function(data){
    
    });
  }]);

 

Give our StoreController access to the products by setting products equal to the data given to us with the http service's success promise.

  app.controller('StoreController', ['$http', function($http){
    var store = this;
    store.products = [];
    $http.get('/store-products.json').success(function(data){
        store.products = data;
    });
  }]);

 

posted @ 2014-07-28 04:52  Zhentiw  阅读(1013)  评论(0)    收藏  举报