Gege
Gege

Reputation: 141

Angular async pipe syntax

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,

Upvotes: 2

Views: 73

Answers (1)

Radik
Radik

Reputation: 2795

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>

Upvotes: 5

Related Questions