简体   繁体   English

Angular 5订阅SwitchMap内部的Observable

[英]Angular 5 Subscribe to an Observable inside SwitchMap

I'm trying to subscribe to an inner Observable. 我正在尝试订阅一个内部Observable。 Here is what I'm doing: 这是我在做什么:

getDates() {
  if (this.booking.id) {

    this.jobService.findById(this.booking.id)
      .map(job => job[0])
      .switchMap((job) => {
        return job ? this.bookingService.findByPropertyId(job.property.id) : Observable.empty();
      }, (job: Job, bookings: Booking[]) => {
        this.mark_jobs_unavailable(job, bookings);
        return job;
      })
      .subscribe(job => {
        this.bookingJob = job;
      });

  }
}

My code is working fine, The only thing I'm trying to achieve is to subscribe to changes of my Bookings as well. 我的代码运行良好,我唯一想要实现的就是也订阅我的预订更改。 Currently as I'm not sure where should I subscribe to the Observable that I get from bookingService.findByPropertyId , My app does not reflect to changes happening in bookings collection. 目前,由于我不确定应该在哪里订阅从bookingService.findByPropertyId获得的Observable,我的应用程序无法反映预订集合中发生的更改。

为了获得有关预订的更改, bookingService.findByProperyId()必须在发生更改的任何时候(即它可以在一段时间内返回多个值bookingService.findByProperyId()返回可观察的值。

You can try something like this: 您可以尝试如下操作:

this.jobService.findById(this.booking.id)
    .mergeAll() // or .mergeAll().first()
    .switchMap(
        job => this.bookingService.findByPropertyId(job.property.id),
        (job, bookings) => [job, bookings]
    )
    .subscribe(([job, bookings]) => {
        this.bookingJob = job;
        this.mark_jobs_unavailable(job, bookings);
    });

Now you have both job and bookings in the subscribe method 现在,您可以在subscribe方法中同时包含工作和预订

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM