【问题标题】:Multiple Observables within Observable not resolved as expectedObservable 中的多个 Observable 未按预期解决
【发布时间】:2018-06-06 07:19:43
【问题描述】:

我有一个简单的问题,我只是没有足够的 Rx 经验。

ngOnInit() {
this.agentService.getAllAgents().subscribe((agents: Agent[]) => {
  this.agents = agents.sort((x: Agent, y: Agent) =>
    Number(new Date(x.date)) - Number(new Date(y.date)));

  for (const agent of this.agents) {
    forkJoin(this.geoLocator.find(agent.address)).subscribe(results => {
      for (const coordinates of results) {
        agent.distance = this.getDistance(<Coordinates>environment.google.COORDINATES, coordinates, Unit.km);
      }
    });
  }
  console.log(this.agents[0]);
  console.log(JSON.stringify(this.agents[0]); 
});

}

这是我得到的结果。

问题是如何以及为什么?

【问题讨论】:

标签: angular observable rxjs5


【解决方案1】:

 public find(agent: Agent): Observable<Agent> {
    return this.http.get<Agent>(`${environment.google.BASE_URL}${agent.address}`)
      .map(response => {
        const coordinates = <Coordinates> {
          latitude: response['results'][0].geometry.location.lat,
          longitude: response['results'][0].geometry.location.lng
        };
        agent.distance = this.getDistance(<Coordinates>environment.google.COORDINATES, coordinates, Unit.km);
        return agent;
      });
  }

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2021-07-06
    • 2018-09-12
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多