代码之家  ›  专栏  ›  技术社区  ›  Adrian Brand

除了ngIf,我可以用异步管道打开一个observable来查看变量吗?

  •  0
  • Adrian Brand  · 技术社区  · 5 年前

    我知道我可以用 *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

    我希望那条线是假的,而不是块消失了。我知道我可以用一个else块,但我需要的是打开一个 readonly$ 指向视图变量的标志 readonly 不想在else块中重复我的整个模板。我想不出任何不涉及订阅组件的方式,但这并不是我唯一一次希望在模板中找到一条路 #boolFlag = boolObs$ | async

    0 回复  |  直到 5 年前
        1
  •  1
  •   Adrian Brand    5 年前

    多亏了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

    推荐文章