【发布时间】:2022-01-26 19:05:09
【问题描述】:
我有一个看起来像这样的模型:
export class Info {
Id: number;
Participant: object;
}
我有一个端点,它获取Info 对象的列表,但它不获取Participant 属性。我需要从不同的服务中获取这些对象,但我无法理解如何使用多个 Observables 以及一个依赖于返回的前一个值的对象。
我正在获取Info 的数组,然后通过该数组从其他服务获取Participant 对象。我将组件的视图部分分配到可观察对象之外,因此在显示值之前遇到问题,直到值实际存在。
部分.html
<tr *ngFor="let connection of connections">
<td> {{connection.Id}} </td>
<td> {{connection.Participant.Name}} </td>
</tr>
组件.ts
ngOnInit() {
this.connectionInfoService.GetInfo().subscribe((connections: Info[]) => {
connections.forEach(x => {
this.participantService.getParticipantByID(x.Id).subscribe((participant) => {
x.Participant = participant;
});
});
this.connections = connections;
});
}
我在控制台中得到Cannot read properties of null (reading 'Name'),因为Name 显然不是null 的属性,直到Participant 实际上有一个值。我怎样才能更好地处理这个问题?我宁愿在我的ngOnInit 服务获取代码中处理它,但我不知道如何处理依赖于另一个类似 Observable 的 Observable。
【问题讨论】:
标签: angular typescript