简体   繁体   English

Angular 9 强制异步 pipe 嵌套在 ngIf 异步 pipe

[英]Angular 9 force async pipe nested in ngIf async pipe

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

How can I force the view to display latest value when condition$ emits true after false?当条件 $ 在 false 之后发出 true 时,如何强制视图显示最新值?


Update更新

condition$ and value$ are both derived from the same formGroup.valueChanges and cannot be instances of BehaviorSubject. condition$ 和 value$ 都派生自同一个 formGroup.valueChanges,不能是 BehaviorSubject 的实例。

What about combining those two and having one observable and subscription like this.将这两者结合起来并拥有一个像这样的可观察和订阅怎么样。 .ts .ts

this.viewObservable$ = combineLatest(
      this.condition$,
      this.value$
    ).pipe(
      map(([condition, value]) => {
        return {
          condition,
          value
        }
      })
    )

and in your.html you can do this在 your.html 中,您可以执行此操作

<ng-container *ngIf="(viewObservable$ | async) as data && data.condition">
  {{ data.value }}
</ng-container>

Does this help?这有帮助吗?

You can do that by adding a shareReplay on your value$ observable:你可以通过在你的value$ observable 上添加一个shareReplay来做到这一点:

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

Or by using a BehaviorSubject or ReplaySubject或者通过使用BehaviorSubjectReplaySubject

you can do as follow to prevent inner subtraction form unsubscribing您可以执行以下操作以防止内部减法取消订阅

<ng-container *ngIf="{val: value$ | async} as source">
  <div *ngIf="condition$ | async">
   {{ source.val }}
  </div>
</ng-container>

example 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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