【问题标题】:Can I unwrap an observable with the async pipe to a view variable other than with an ngIf?我可以使用异步管道将 observable 解包到视图变量而不是使用 ngIf 吗?
【发布时间】:2021-03-19 04:37:03
【问题描述】:

我知道我可以使用*ngIf="obs$ | async as obs",但是如果我需要在 obs 为 false 时使用该值怎么办?

<ng-container *ngIf="obs$ | async as obs">
  observable is {{ obs | json }}
</ng-container>

https://stackblitz.com/edit/angular-zkbemb?file=src%2Fapp%2Fapp.component.html

我希望该行读取 observable 是 false 而不是块消失。我知道我可以使用 else 块,但我需要将 readonly$ 标志解包到视图变量 readonly 并且不想在 else 块中重复我的整个模板。我想不出任何不涉及订阅组件的方式,但这并不是我发现自己想要在模板中找到一种方式去#boolFlag = boolObs$ | async

的唯一场合

【问题讨论】:

    标签: angular async-pipe


    【解决方案1】:

    感谢对 Reddit 帖子的评论

    https://www.reddit.com/r/Angular2/comments/m2r39u/angular_ngif_learn_all_the_features_available_in/gqku9f9/

    <ng-container *ngIf="{ obs: obs$ | async } as data">
      observable is {{ data.obs | json }}
    </ng-container>
    

    https://stackblitz.com/edit/angular-ghxtsn?file=src%2Fapp%2Fapp.component.html

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2019-08-02
      • 2020-11-13
      • 1970-01-01
      • 2023-03-16
      • 2019-11-25
      相关资源
      最近更新 更多