【问题标题】:ForkJoin Observables from http.get来自 http.get 的 ForkJoin Observables
【发布时间】:2018-12-10 20:55:33
【问题描述】:

我收到了很多http.get 电话,我得到了不同的分数。 我需要将它们全部放在数组中并在网格中打印:

我就是这样做的:

当用户输入interval时:

ngOnChanges() {
if (this.interval) {

  for (let id of this.ids) {
    this.loadData(id);
  }
}
this.initializeCols();

}

我调用 loadData(id); 方法,它使用 ForkJoin 创建数据:

  private loadData(id: string) {
let url = DataService.url+ '/data/' + id + '/points';

// Generate API requests
let observables = [];
observables.push(this.http.get(url, DataService.contentTypeJsonHeaders).map(r => r.json()));

// const myPromise = val =>
// new Promise(resolve =>
//   setTimeout(() => resolve(`Promise Resolved: ${val}`), 10000)
// );

// of(observables).pipe(mergeMap(q => forkJoin(...q.map(myPromise)))).subscribe(
//   val => this.model.push(val));

Observable.forkJoin(observables).subscribe((response: any[]) => {
  for (let i = 0; i < response.length; i++) {
    let dataArray: Datapoints[] = ArrayFlattener.Inflate(response[i].data);
    for (let d of dataArray) {
      const temp = {
        id: id,
        time: d.time,
        value: d.value,
        originalTime: d.time,
        originalValue: d.value,
        interval: d.interval,
        metric: DataService.GetById(stream.metricId).name
      };
      this.model.push(temp);
    }
  }
});

}

但我不能等到model 完全填满数据,然后再将其打印到网格中。我在其他尝试中在loadData() 方法中留下了一些注释掉的代码。问题是我在网格中看不到模型数据,除非我单击“添加新”按钮或其他东西,以便它会进行另一个调用,因为它应该仅在所有调用完成时才填充网格。我的 ForkJoin 做错了什么?

【问题讨论】:

    标签: angular typescript observable angular2-observables fork-join


    【解决方案1】:

    您不应将 observables 作为单个数组参数传递,而应将其作为多个参数传递。

    尝试使用spread operatorObservable.forkJoin(...observables)

    【讨论】:

    • 虽然没有帮助:(
    猜你喜欢
    • 2019-02-17
    • 2021-03-10
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多