When doing some synchronous actions that contain heavy calculations, showing something before and after might be a problem.
Component template:
<div>
<my-spinner *ngIf="myService.isLoading"></my-spinner>
</div>
Service
@Injectable()
export class MyService {
private isLoading = false;
constructor() {}
myFunction() {
this.isLoading = true;
this.calculateSomethingSuperHeavyThatTakes5seconds();
this.isLoading = false;
}
}
executing myFunction actually wouldn't show the spinner.
One way I fixed that was wrapping the heavy function with setTimeout like this:
myFunction() {
this.isLoading = true;
setTimeout(() => {
this.calculateSomethingSuperHeavyThatTakes5seconds();
this.isLoading = false;
});
}
And it works but it's not a clean solution for me. applicationRef.tick() in myFunction(), wrapping myFunction content in this.zone.run() or changeDeetctionRef.detectChanges() variations didn't fix the problem.