简体   繁体   English

我可以使用 async pipe 将 observable 解包到除 ngIf 之外的视图变量吗?

[英]Can I unwrap an observable with the async pipe to a view variable other than with an ngIf?

I know I can use *ngIf="obs$ | async as obs" but what if I need to use the value when obs is false?我知道我可以使用*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 https://stackblitz.com/edit/angular-zkbemb?file=src%2Fapp%2Fapp.component.html

I want that line to read observable is false instead of the block disappearing.我希望该行读取 observable 是错误的,而不是块消失。 I know I could use an else block but what I need is to unwrap a readonly$ flag to a view variable readonly and don't want to repeat my whole template in an else block.我知道我可以使用 else 块,但我需要将readonly$标志解包为readonly视图变量,并且不想在 else 块中重复我的整个模板。 I can't think of any way that doesn't involve subscribing in the component but this is not the only occasion had have found myself wanting a way in the template to go #boolFlag = boolObs$ | async我想不出任何不涉及订阅组件的方式,但这并不是唯一一个发现自己想要在模板中找到 go #boolFlag = boolObs$ | async #boolFlag = boolObs$ | async

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

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