Angularjs multiple $http.get request

前端 未结 3 1787
渐次进展
渐次进展 2021-01-29 23:35

I need to do two $http.get call and I need to send returned response data to my service for doing further calculation.

I want to do something like below:

3条回答
  •  没有蜡笔的小新
    2021-01-30 00:07

    I had a similar problem recently so I'm going to post my answer also:

    In your case you only have two calculations and it seems to be the case this number is not mutable.

    But hey, this could be any case with two or more requests being triggered at once.

    So, considering two or more cases, this is how I would implement:

    var requests = [];
    requests.push($http.get('FIRSTRESTURL', {'cache': false}));
    requests.push($http.get('SECONDRESTURL', {'cache': false}));
    
    $q.all(requests).then(function (responses) {
      var values = [];
      for (var x in responses) {
        responses[x].success(function(data){
          values.push(data);
        });
      }
      $scope.results = MyService.doCalculation(values);
    });
    

    Which, in this case, would force doCalculation to accept an array instead.

提交回复
热议问题