I'm using Angular 4 and I'd like to display a loading screen (basic spinner) between routes navigation. I've used this technique in order to subscribe to the proper events, and used style.visibility in order to show/hide my spinner.
For some reason, although I'm correctly detecting the start and end events of the navigation, my spinner will not show. In fact, although the visibility field is changing correctly, the spinner will still not show.
The only way I managed to display it is by forcing it to be set to 'visible' instead of 'hidden' at all times.
Attached are relevant sections of my code:
app.component.ts:
constructor (private utils: UtilsService, private router: Router) {
// Intercept all navigation events
router.events.subscribe((event: RouterEvent) => {
this.navigationInterceptor(event)
})
// Calls our initializer
utils.init();
}
/* Intercept the navigator in order to display a loading screen */
navigationInterceptor(event: RouterEvent): void {
// Starting the navigation
if (event instanceof NavigationStart) {
this.loading = true;
}
// The navigation ended
if (event instanceof NavigationEnd) {
this.loading = false;
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
if (event instanceof NavigationCancel) {
this.loading = false;
}
if (event instanceof NavigationError) {
this.loading = false;
}
// Get our loading container
var loading_container = document.getElementById('loader-container');
// If our loading container isn't available, this is the application first launch (ie. full refresh)
// so we need to display a full loading logo
if (loading_container) {
// This is just a redirect, not a full refresh, so just hide or show the loading screen
document.getElementById('loader-container').style.visibility = this.loading ? 'visible' : 'hidden';
}
}
app.component.html:
<div style="position: fixed; left: 220px; width: calc(100% - 220px); height: 100%; z-index: 10000; background-color: rgba(255, 255, 255, 0.6);" id = "loader-container">
<div style = "top: 30%;" class="sk-spinner sk-spinner-double-bounce">
<div class="sk-double-bounce1"></div>
<div class="sk-double-bounce2"></div>
</div>
</div>
<!-- Main view/routes wrapper-->
<router-outlet>
</router-outlet>