I'm trying to add a loading spinner on each http.get
action in my app.
Here is my http.get :
protected get(url: string): any {
//Loading start
this.sessionService.showLoader();
(...)
}
The SessionService
call my LoadingService
:
constructor(private cookieService: CookieService, private loadingService : LoadingService) {
}
public showLoader(): void {
this.loadingService.show();
}
public hideLoader(): void {
this.loadingService.hide();
}
here is my service LoadingService
import { Injectable, OnInit, EventEmitter } from '@angular/core';
@Injectable()
export class LoadingService implements OnInit {
public loadingEvent: EventEmitter<boolean>;
constructor() {
this.loadingEvent = new EventEmitter();
}
ngOnInit() {
this.loadingEvent.emit(false);
}
show() {
this.loadingEvent.emit(true);
}
hide() {
this.loadingEvent.emit(false);
}
}
Then, here is a part of my main layout component :
showLoadingDiv : boolean;
constructor(public loadingService : LoadingService) {
}
ngOnInit() {
}
ngAfterViewInit(){
this.loadingService.loadingEvent.subscribe((res) => {
this.showLoadingDiv = res;
});
}
And finally, in my Html layout template :
<div class="loaddiv" *ngIf="showLoadingDiv">
Loading...
</div>
When others modules are load fast, I do not have the error ExpressionChangedAfterItHasBeenCheckedError
but If a module take a little time (because huge data), I've got this error.
I'm already see this SO but not work for me : https://stackoverflow.com/a/38846793/1729211