我有一个forkjoin
,有2个通话。在html
中,我需要等待forkjoin
的响应到达。有什么方法可以使用async
管道吗?我在另一篇文章中看到async
仅适用于observables
和promises
。我尝试使用*ngIf
来执行此操作,但是它不起作用。预先感谢
TS
var1: string;
var2: string;
forkJoin(
this.call1(),
this.call2()
).subscribe(([res1, res2]) => {
this.var1 = res1;
this.var2 = res2;
}, err => console.log(err));
HTML
<div class="col">
<label class="labelEsp">Var 1:</label>
<span class="spanEsp uppercase" >{{ var1 }}</span>
</div>
<div class="col">
<label class="labelEsp"> Var 2:</label>
<span class="spanEsp" >{{ var2 }}</span>
</div>
答案 0 :(得分:1)
您可以继续使用typescript
中的订阅,并使用标志在视图中隐藏和显示HTML。
类似的事情会做:
dataReady = false;
forkJoin(
this.call1(),
this.call2()
).subscribe(([res1, res2]) => {
this.var1 = res1;
this.var2 = res2;
},
(err) => console.log(err),
() => {
this.dataReady = true
// complete block
}
);
<ng-container *ngIf="dataReady else loading">
<div class="col">
<label class="labelEsp">Var 1:</label>
<span class="spanEsp uppercase" >{{ var1 }}</span>
</div>
<div class="col">
<label class="labelEsp"> Var 2:</label>
<span class="spanEsp" >{{ var2] }}</span>
</div>
</ng-container>
<ng-template #loading>
loading...
</ng-template>
如果您想使用async
管道本身进行操作,则可以使用以下内容:
data = forkJoin(
this.call1(),
this.call2()
)
<ng-container *ngIf="data | async as resolvedData else loading">
<div class="col">
<label class="labelEsp">Var 1:</label>
<span class="spanEsp uppercase" >{{ resolvedData[0] }}</span>
</div>
<div class="col">
<label class="labelEsp"> Var 2:</label>
<span class="spanEsp" >{{ resolvedData[1] }}</span>
</div>
</ng-container>
<ng-template #loading>
loading...
</ng-template>
在此处查看示例:https://stackblitz.com/edit/angular-uwpwya?file=src%2Fapp%2Fapp.component.html