Angular 2 - Show loading-information when (observableData | async) is not yet resolved
This is how I do it. Also i use $
at the and of the variable name to remind me that it is a stream.
// dataview.html
<div *ngIf="isLoading$ | async">Loading data...</div>
<ul *ngIf="!(isLoading$ | async)">
<li *ngFor="let d of data">{{ d.value }}</li>
</ul>
// dataview.ts
data: any[] = [];
isLoading$: BehaviorSubject<boolean> = new BehaviorSubject(false);
getData() {
this.isLoading$.next(true);
this._api.getData().subscribe(
data => {
this.data = data;
},
error => {
this.error = error;
},
complete => {
this.isLoading$.next(false);
});
}
I Came up with the following:
export enum ObsStatus {
SUCCESS = 'Success',
ERROR = 'Error',
LOADING = 'Loading',
}
export interface WrapObsWithStatus<T> {
status: ObsStatus;
value: T;
error: Error;
}
export function wrapObsWithStatus<T>(obs: Observable<T>): Observable<WrapObsWithStatus<T>> {
return obs.pipe(
map(x => ({ status: ObsStatus.SUCCESS, value: x, error: null })),
startWith({ status: ObsStatus.LOADING, value: null, error: null }),
catchError((err: Error) => {
return of({ status: ObsStatus.ERROR, value: null, error: err });
})
);
}
And then in your component:
TS
public ObsStatus: typeof ObsStatus = ObsStatus;
public obs$: Observable<WrapObsWithStatus<YOUR_TYPE_HERE>> = wrapObsWithStatus(this.myService.getObs());
HTML
<div *ngIf="obs$ | async as obs" [ngSwitch]="obs.status">
<div *ngSwitchCase="ObsStatus.SUCCESS">
Success! {{ obs.value }}
</div>
<div *ngSwitchCase="ObsStatus.ERROR">
Error! {{ obs.error }}
</div>
<div *ngSwitchCase="ObsStatus.LOADING">
Loading!
</div>
</div>
I did it by using the async pipe. But this approach still required you to catch it manually to handle the error. See here for more detail.
app.component.html
<div class="wrapper">
<div class="form-group" *ngIf="pickupLocations$ | async as pickupLocations; else loading">
<ul class="dropdown-menu" *ngIf="pickupLocations.length">
<li *ngFor="let location of pickupLocations">
<strong>{{location.Key}}</strong>
</li>
</ul>
<span *ngIf="!pickupLocations.length">There are no locations to display</span>
</div>
<ng-template #loading>
<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
</ng-template>
</div>
app.component.ts
this.pickupLocations$ = this.apiService.getPickupLocations(storeId);