2

Is there a way to factorize the

(places$ | async) 

in

  <div *ngIf="(places$ | async) === undefined">Loading...</div>
  <div *ngIf="(places$ | async) === null">No data</div>
  <div *ngIf="(places$ | async) != null">{{ (places$ | async) }}</div>

I indeed store an array of places in my observable, and I need to check if the observable is either undefined or null or not empty.

Best regards,

1 Answer 1

5

you can use *ngIf to do as follow

i wrap it in {} to be sure we get data anyway and not be affected by ngIf

<ng-container *ngIf="{ data: places$ | async} as source">
  <div *ngIf="source.data === undefined">Loading...</div>
  <div *ngIf="source.data === null">No data</div>
  <div *ngIf="source.data !== undefined && data !== null">{{ source.data }}</div>
</ng-container>
Sign up to request clarification or add additional context in comments.

2 Comments

"Loading..." is never displayed with your solution.
@alcfeoh it depends from source if we use new BehaviorSubject(undefined) it will, if we use Subject and do source.next(undefined) it also will show loading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.