Search code examples
angularjsangular-resource

How to pass data between queries in angular resource?


Here is my example this is working fine:

var first  = $http.get("/app/data/first.json"),
    second = $http.get("/app/data/second.json"),
    third  = $http.get("/app/data/third.json");

$q.all([first, second, third]).then(function(result) {
  var tmp = [];
  angular.forEach(result, function(response) {
    tmp.push(response.data);
  });
  return tmp;
}).then(function(tmpResult) {
  $scope.combinedResult = tmpResult.join(", ");
});

Here the first, second and third all work alone without depended. In case for example, 'secondrequires someidfromfirstandthirdrequires somedatafromsecond... then how to initiate therequest` with depend on other?

var first  = $http.get("/app/data/first.json"), //fetching id
    second = $http.get("/app/data/second.json"), //needs some id from first
    third  = $http.get("/app/data/third.json"); //needs some data from second

How that should be handled. also, the query will from by series of adding the request or by series of adding in $q.all.

any one explain me with updating my code please?


Solution

  • first.then(function(data1){
      second.then(data2){
        third.then(data3){
    
        });
      });
    });
    

    first,second and third are your promises. Error can be received in second callback function like so.

    promise.then(function(successData){
      // success here
    }, function(errorData){
      // error here.
    });