I have a component for the role of loading screen. I wanna show it during api calls.
loading-screen.component.html
<div *ngIf="isLoading | async" class="overlay">
//Some html for the loading screen.
</div>
loading-screen.component.ts
isLoading: Subject<boolean> = this.loaderService.isLoading;
Btw when I set isLoading to true hardcoded, component fade in and I see my loading screen as it should be. So I assume there isn't anything wrong with this component.
loading-screen.interceptor.ts
@Injectable()
export class LoaderInterceptor implements HttpInterceptor {
constructor(public loaderService: LoadingScreenService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.loaderService.show();
return next.handle(req).pipe(
finalize(() => this.loaderService.hide())
);
}
}
I am not sharing the api call but it shouldn't be necessary since I see that show() and hide() methods triggered when I make my api call while debuging.
loading-screen.service.ts
@Injectable({
providedIn: 'root'
})
export class LoadingScreenService {
isLoading = new Subject<boolean>();
constructor() { }
show() {
this.isLoading.next(true);
}
hide() {
this.isLoading.next(false);
}
}
app.module.ts
providers: [
LoadingScreenService,
{ provide: HTTP_INTERCEPTORS, useClass: LoaderInterceptor, multi: true }
]