【发布时间】:2019-02-13 08:32:22
【问题描述】:
我需要从服务中获取数据。然后我需要遍历数据并使用我从第一次调用中获得的 id 获取数据。
我的目标是在加载所有数据时通过在 ChangeDetectorRef 上调用 markForCheck() 来优化 Angular 绑定性能。
这是我当前的代码。它完成了工作。但是对 markForCheck() 的调用是在所有数据加载之前完成的。
this.subs.push(
this.supplierService
.supplier(this.supplierId)
.pipe(
tap(supplier => {
this.subs.push(
of(supplier.employees.map(emp => emp.authId))
.pipe(
mergeMap(ids => ids.map(id => this.authRegisterService.lookupId(id))),
mergeMap(res => res)
)
.subscribe(
result => {
this.authRegs.push(result);
},
error => {
this.toastr.error(error);
return EMPTY;
}
)
);
}),
tap(supplier => {
this.subs.push(this.cvrService.getCompany(supplier.cvr).subscribe(cvr => (this.cvr = cvr)));
}),
tap(supplier => {
this.subs.push(
of(supplier.locations.map(loc => loc.sorId))
.pipe(
mergeMap(ids => ids.map(id => this.sorService.getLocation(id))),
mergeMap(res => res)
)
.subscribe(sor => {
this.sors.push(sor);
})
);
})
)
.subscribe(supplier => {
this.supplier = supplier;
this.supplierStatusLabel = SupplierStatusNames.get(supplier.status);
this.cd.markForCheck();
})
);
我阅读了forkJoin 的文档,但我不知道如何将它与第一次通话结合起来。非常感谢所有输入。
【问题讨论】: