angularjs - Returning one object with the result of two AJAX GET calls -


i'm working on simple weather app grab both current , weekly forecast weather api. keep simple, i'd weatherservice function, getforecast, somehow make 2 ajax calls -- 1 weekly forecast, have, , 1 current forecast (unfortunately don't think api has means of retrieving json return contains both). i'm not sure best way go doing this, i'm new angular.

here's service:

weather.service('weatherservice', function($resource, $http){      this.currentforecast = null;      // default city     this.city = 'chicago, il';      this.getforecast = function(location, type) {              return $http({                 method : "get",                 url : "http://api.openweathermap.org/data/2.5/forecast/daily?q="+location+"&mode=json&cnt=7&appid=e92f550a676a12835520519a5a2aef4b"             }).then(                  function(response) {                      return response.data;                  }             )     };  }); 

and i'd second get, retrieving from: http://api.openweathermap.org/data/2.5/weather?q=chicago,il&appid=e92f550a676a12835520519a5a2aef4b appended response, there's single object returned.

also, if isn't best way go doing this, i'm open suggestions.

what looking angular promises library $q

$q.all([$http(...), $http(...),...]).then(function(ret){     // ret has results ajax calls          }) 

more specifically:

weather.service('weatherservice', function($resource, $http, $q){    this.getforecast = function(location, type) {      return $q.all([          $http.get(url1(location, type)),           $http.get(url2(location, type))      ])   }  })   ...   weatherservice.getforcast(location, type).then(function(ret){      console.log(ret[0].data)     console.log(ret[1].data)      }) 

there excellent video on using $q.all @ egghead.io


Comments