gpt4 book ai didi

javascript - 如何在 AngularJS 中链接两个 http 调用?

转载 作者:行者123 更新时间:2023-11-30 15:13:10 25 4
gpt4 key购买 nike

我正在尝试链接两个 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
...

我需要改变什么才能做到这一点?

非常感谢!!

最佳答案

您要做的是在检索到额外详细信息后修改每个“飞行记录” 条目。您还可能希望使用 $q.all 向调用者发出操作已完成的信号。

const promise = flightRecordService.query().$promise.then(flightRecords => {
return $q.all(flightRecords.map(flightRecord => {
return financeDocumentService.isReferencedDocumentIdCompensated({
id: flightRecord.id
}).$promise.then(data => Object.assign(flightRecord, {
isCompensated: data.headers.compensated
}))
}))
})

promise.then(flightRecords => {
$scope.flightRecords = flightRecords
})

关于javascript - 如何在 AngularJS 中链接两个 http 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44837224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com