I have the following code:
public obs$: Observable<boolean>
<div *ngIf="(obs$ | async) === true || (obs$ | async) === false">
{{ (obs$ | async) ? 'yes' : 'no' }}
</div>
It works as intended, but the if
looks a little verbose.
The problem is that I cannot simply do <div *ngIf="(obs$ | async)">
. If I try that, it will work in the case when the observable did not emit a value yet or if the value is true, but it will not work if the value is false, because the if will evaluate to false and the div is not displayed.
I assume the same issue applies if a falsy value is returned, such as an empty string or 0.
Is there a better, easier way of doing that?
(obs$ | async) !== null
– SedgewakeObservable
will not emit a value? – Subtropicspipe
the response and return afalse
in thecatchError
operator. That way you'd always be certain that a boolean value is emitted. And you won't have to use an*ngIf
on thediv
– Subtropics