![](/img/trans.png)
[英]Angular 2 & 4: NgIf/else condition for showing directives on window load
[英]Angular: How to put 2 async subscriptions in ngIf with common else condition
我有一個用例,其中我有 2 個具有常見 else 條件的異步訂閱。
<ng-container *ngIf="{ data1: data1$ | async, data2: data2$ | async } as history; else loader">
<h2>Inside ngIf condition</h2>
</ng-container>
<ng-template #loader>Loader here....</ng-template>
這里發生的是異步訂閱中的內容甚至在訂閱之前就顯示出來了。 我知道這是因為多個訂閱的 object 字面定義。
有沒有辦法來解決這個問題?
像你寫的東西很模棱兩可。
我不確定您要達到什么語義。
合並 Observable 的方法有很多(merge、withLatestFrom、forkJoin、zip 等)。
您可能必須自己選擇並實施一個。 請務必閱讀文檔。
例如, forkJoin
[doc]在其源 Observables 全部完成之前不會發出。
在您的組件中:
joinedData$ = forkJoin({
data1: this.data1$,
data2: this.data2$
});
標記
<ng-container *ngIf="(joinedData$ | async) as history; else loader">
<h2>Inside ngIf condition</h2>
</ng-container>
<ng-template #loader>Loader here....</ng-template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.