链接两个HTTP调用

链接两个HTTP调用

我正在尝试链接两个http调用。第一个返回一组记录,然后我需要获取每个记录的财务数据。

flightRecordService.query().$promise.then(function (flightRecords) {
  $scope.flightRecords = flightRecords;
  for (var i = 0; i < $scope.flightRecords.length; i++) {
    $scope.flightRecords[i].financeDocument =
      financeDocumentService
      .isReferencedDocumentIdCompensated({
        id: $scope.flightRecords[i].id
      }).$promise.then(
        function (data) {
          return ({
            'isCompensated': data.headers['compensated']
          });

        }
      );
    console.log($scope.flightRecords);
  }
});


这是FlightRecord对象:

$$hashKey: "object:27"
aircraft: {id: 100, registration: "LV-OEE", model: "152", status: "ACTIVE", brand: "Cessna", …}
amountOfHours: 1
canceled: false
closed: false
crew: [Object] (1)
destiny: null
endFlight: "2017-01-06T20:54:05.296"
financeDocument: d
  --> $$state: {status: 1, value: {isCompensated: "false"}}
  --> d prototipo
id: 100
landings: 0
nature: "LDI"
opened: true
origin: null
purpose: "VP"
startFlight: "2017-01-06T19:44:05.296"
status: "OPENED"
type: "ENT"


financeDocument对象的结构不符合我的期望...我需要以下格式:

...
endFlight: "2017-01-06T20:54:05.296"
financeDocument: { isCompensated: "false" }
id: 100
...


我需要改变以获得什么?

非常感谢!!

最佳答案

为什么不将其设置在原始对象上?

flightRecordService.query().$promise.then(function (flightRecords) {
  $scope.flightRecords = flightRecords;
  for (var i = 0; i < $scope.flightRecords.length; i++) {
      (function(record) {
          financeDocumentService
          .isReferencedDocumentIdCompensated({
            id: $scope.flightRecords[record].id
          }).$promise.then(
            function (data) {
              $scope.flightRecords[record].financeDocument = {
               'isCompensated': data.headers['compensated']
              }
            });
    })(i)
    console.log($scope.flightRecords);
  }
});

07-26 03:59