[Fixed] 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?

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


I want that line to read observable is false instead of the block disappearing. 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. 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


Thanks to this comment on a Reddit post


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


Leave a Reply

(*) Required, Your email will not be published