angular — accessing data of multiple http calls - how to resolve the promises

前端 未结 1 1866
逝去的感伤
逝去的感伤 2020-11-28 19:30

I am getting stuck on something I think should be straight forward. I need to take data from three different ajax calls, combine and process all three, and display the resu

相关标签:
1条回答
  • 2020-11-28 20:07

    You can use $q's function 'all':

    function giftControler ($scope, $http, $q) {
      var names = $http.get("names.json"),
          naughty = $http.get("naughty.json"),
          nice = $http.get("nice.json");
      $q.all([names, naughty,nice]).then(function(arrayOfResults) { 
          ... This callback would be called when all promised would be resolved
        });
    

    This way is little bit cleaner.

    Here is link to docementation: http://docs.angularjs.org/api/ng.$q

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