【发布时间】:2023-03-29 16:25:01
【问题描述】:
我想在带有另一个异步管道的角度模板中使用异步管道。
问题是我的第二个异步管道返回null,所以没有打印。但是我的第一个异步管道运行良好。
那么我该如何在我的 html 模板中打印我的 zone.title 呢?我只能通过doSomething(id) 方法获得它。
template.component.html
<div *ngFor="let site of sites$ | async">
<p>{{site.title}}</p>
<div *ngIf="doSomething(site.id) | async as zone">
<p>{{zone?.title}}<p>
</div>
</div>
template.component.ts
public sites$: Observable<{id: string, title: string}[]>;
constructor(private siteService: SiteService) {}
ngOnInit() {
this.sites$ = this.siteService.getSites();
}
doSomething(id: string): Observable<{id: string, title: string}> {
return this.siteService.getZoneById(id);
}
site.service.ts
constructor(private http: HttpClient) {}
getSites(): Observable<{id: string, title: string}[]> {
this.http.get(...);
}
getZoneById(id: string): Observable<{id: string, title: string}> {
this.http.get(...);
}
【问题讨论】:
标签: angular rxjs observable