AngularJS return two http get requests only when resolved

前端 未结 1 1609
情话喂你
情话喂你 2021-01-26 09:29

I have an Angular app, and in the controller I need to call a function which makes two http get requests, and I need this function to return these values just when they are reso

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 10:28

    I would use $q.all to return combined promise from Service, and process result in controller. e.g.:

    function myService($http, $q, API_EVENT1, API_EVENT2) {
      return {
        getExtras: getExtras
      }
      function getExtras(name) {
        return $q.all([
          $http({method: 'GET', url: API_EVENT1 + '/' + name}), 
          $http({method: 'GET', url: API_EVENT2 + '/' + name})])
         );
      }
    }
    
    function myController(info) {
      var vm = this;
      vm.name = info.data.name;
    
      MyService.getExtras(name).then(function(data) {
        vm.extra_data.first = data[0];
        vm.extra_data.second = data[1];
      });
    }
    

    0 讨论(0)
提交回复
热议问题