As part of my work on an Angular app, I am developing a loader to enhance user experience.
While the typical approach involves utilizing a boolean parameter for subscribing to HTTP requests, in my case, the service's response consists of multiple image URLs since the page is heavily image-based.
However, I encountered an issue where the loader stops before all images finish loading due to slower network connections, causing frustration among users.
In an attempt to address this, I experimented with JavaScript's load event to monitor when all resources have completed loading and then stop the loader. Unfortunately, it seems I'm unable to modify the loader's status from within the listener function.
Below is a snippet of what I've tried:
// The TypeScript component
isLoading: boolean;
ngOnInit() {
this.isLoading = true;
this.checkIfLoaded();
}
checkIfLoaded() {
window.addEventListener("load", function (event) {
console.log("All resources finished loading!");
// At this point, I should take action like returning false or...
// ...adjusting the isLoading value, but accessing isLoading here appears difficult.
});
}
// The template
<ng-container *ngIf="isLoading">
<app-spinner></app-spinner>
</ng-container>
Environment: Angular 4.4 Any assistance or insights on this matter would be greatly appreciated! Thank you.