首页 > 解决方案 > Angular 9强制异步管道嵌套在ngIf异步管道中

问题描述

<ng-container *ngIf="condition$ | async">
    {{ value$ | async }}
</ng-container>

当 condition$ 在 false 之后发出 true 时,如何强制视图显示最新值?


更新

condition$ 和 value$ 都派生自同一个 formGroup.valueChanges,不能是 BehaviorSubject 的实例。

标签: angular

解决方案


您可以通过shareReplayvalue$observable 上添加 a 来做到这一点:

<ng-container *ngIf="condition$ | async">
    {{ value$ | async }}
</ng-container>
value$ = obs$.pipe(
  shareReplay(1)
);

或通过使用BehaviorSubjectReplaySubject


推荐阅读