제출을 클릭하면 선택한 모든 날짜에 대해 여러 HTTP 요청을 만들고 싶습니다. 아래 코드는 제출을 클릭했을 때 selectedDate의 마지막 요소 만 booking.bookDate에 할당합니다.
selectedDate: any = [];
booking: SaveBooking = {
id: 0,
roomId: 0,
buildingId: 0,
bookDate: '',
timeSlots: [],
modules: [],
semesterId: 0,
};
submit() {
var result$;
for (let date of this.selectedDate) {
this.booking.bookDate = date;
result$ = this.bookingService.create(this.booking);
}
}
result$.subscribe(() => {
...this.toasty.success()
});
모델> booking.ts :
export interface SaveBooking {
id: number;
semesterId: number;
roomId: number;
buildingId: number;
bookDate: string;
timeSlots: number[];
modules: number[];
}
서비스> booking.service.ts :
create(booking) {
return this.http.post(this.bookingsEndpoint, booking)
.pipe(map(response => response));
}
mergeMap () 및 toArray ()를 사용하여이 작업을 더 잘 수행 할 수 있습니다. ForkJoin은 호출이 실패하면 취소됩니다.
submit() {
const result$ =
from(this.selectedDate)
.pipe(
mergeMap(date => {
this.booking = {...this.booking, bookDate: date};
return this.bookingService.create(this.booking);
}),
toArray()
);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다