2016-02-26 55 views
0

我想在各自的表中显示该设置中的所有设置和雇员人数。因此,我想创建两个不同的http(Get)调用并追加这些结果。所以我的一个朋友建议我去promise.push。我尝试了不同的方式,但我失败了。无法在javascript中添加promise.push数据

<html> 
 
<table class="table"> 
 
    <tbody> 
 
    <tr ng-repeat="setting in createAttendance.AtSettings"> 
 
     <td> 
 
     <div class="col-lg-12"> 
 
      <p class="leavesettingpara"> 
 
      <strong><span class="parastrong"></span>{{setting.name}}</strong> 
 
      </p> 
 
     </div> 
 
     <div class="col-lg-12"> 
 
      <div class="col-lg-3"> 
 
      
 
      <p class="leavesettingpara">{{settings.numOfEmployees}} Employees</p> 
 
      </div> 
 
      <div class="col-lg-3"> 
 
      <p class="leavesettingpara">{{setting.date | date:'medium'}}</p> 
 
      </div> 
 
      <div class="col-lg-3"> 
 
      <p class="leavesettingpara">By {{setting.createdName}}</p> 
 
      </div> 
 
     </div> 
 
     </td> 
 

 
    </tr> 
 

 
    </tbody> 
 
</table> 
 

 
</html>

控制器:

angular.module('myApp') 
 
    .controller('settingsDisplayController', ['$state', 'attendanceSettingsService' 
 

 
    function($state, attendanceSettingsService) { 
 
     attendanceSettingsService.getSettings().then(function(data) { 
 
     vm.AtSettings = data; 
 
     console.log("organization" + data); 
 
     }); 
 
    } 
 
    ]);

我面临的主要问题是:“我想与结果第一HTTP调用的父子relation.On成功.id我们想要进行第二次http呼叫“

服务:

angular.module('myApp') 
 
    .service('attendanceSettingsService', ['$q', '$http', '$state', 
 
    function($q, $http, $state) { 
 
     this.getSettings = function() { 
 
     var d = $q.defer(); 
 
     $http.get('api/organizationName').success(function(result) { 
 
      var promises = []; 
 
      var defer = $q.defer(); 
 
      var res = []; 
 
      promises.push($http.get('api/organizations/' + result.id + '/attendanceProfileSettings').success(function(res) { 
 
      console.log(res); 
 
      })); 
 
      console.log(res); 
 
      promises.push($http.get('api/setting/' + res.id + '/employees')); 
 

 

 
      $q.all(promises).then(function(data, status, headers, config) { 
 
      console.log(data); 
 
      d.resolve(data); 
 

 
      }, function(data, status, headers, config) { 
 
      console.log(" failure...." + status); 
 
      }); 
 

 
      return defer.promise; 
 

 
     }); 
 

 
     return d.promise; 
 
     }; 
 
    } 
 
    ]);

+0

你可能想使用承诺链接。请查看http://www.html5rocks.com/en/tutorials/es6/promises/#toc-promises-queues – jbrown

回答

0

,当你有很多相似之处承诺这种策略被使用,并且希望当所有的承诺都得到圆满解决的回调。

你可能想要什么,它被称为承诺链接它调用下一个答案与前面的答案。例如:

travelService 
      .getDeparture(user)           // Request #1 
      .then(function(departure) 
      { 
       $scope.departure = departure;        // Response Handler #1 
       return travelService.getFlight(departure.flightID);  // Request #2 
      }) 
      .then(function(flight) 
      { 
       $scope.flight = flight;          // Response Handler #2 
       return weatherService.getForecast($scope.departure.date); // Request #3 
      }) 
      .then(function(weather) 
      { 
       $scope.weather = weather;         // Response Handler #3 
      }); 

这是一个承诺链接引导你可能想看看一个例子:http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/