简体   繁体   English

Angular:如何在具有常见 else 条件的 ngIf 中放入 2 个异步订阅

[英]Angular: How to put 2 async subscriptions in ngIf with common else condition

I have a use case wherein I have 2 async subbscriptions with a 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>

What's happening here is the content inside the async subscription is being displayed even before the subscription.这里发生的是异步订阅中的内容甚至在订阅之前就显示出来了。 I understand that is because of the object literal definition of multiple subscriptions.我知道这是因为多个订阅的 object 字面定义。

Is there a way to fix this?有没有办法来解决这个问题?

The problem问题

Something like what you've written is pretty ambiguous.像你写的东西很模棱两可。
I'm not sure what semantics you're trying to achieve.我不确定您要达到什么语义。

There are many ways to merge Observables (merge, withLatestFrom, forkJoin, zip, etc).合并 Observable 的方法有很多(merge、withLatestFrom、forkJoin、zip 等)。
You'll likely have to pick and implement one yourself.您可能必须自己选择并实施一个。 Be sure to read the documentation.请务必阅读文档。

Example:例子:

forkJoin [doc] , for example, doesn't emit until its source Observables all complete.例如, forkJoin [doc]在其源 Observables 全部完成之前不会发出。

In your component:在您的组件中:

joinedData$ = forkJoin({
  data1: this.data1$,
  data2: this.data2$
});

Markup标记

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM