3

TLDR; I have a "spinner" HTML element in my index.html, and I want to show it until the @angular/router resolve() has been resolved, how to achieve that?

More details:

When not using router and resolves, I can achieve what i expect just doing this:

index.html:

<html>
<head>...</head>
<body>
  <app-base>
    <div class="spinner">Loading...</div>
  </app-base>
</body>

What happens here: a spinner is showing until Angular2 application bootstraps, and replaces the content of <app-base>. Everything works wonders.

With @angular/router:
index.html:

<html>
<head>...</head>
<body>
  <app-base>
    <div class="spinner">Loading...</div>
  </app-base>
</body>

app.component.ts: (code is exactly the same as suggested here, setting this.loading)

@Component({
  selector: 'app-base',
  template: `
    <div class="spinner" *ngIf="loading">Loading...</div>
    <router-outlet></router-outlet>
  `,
})

routing.ts:

export const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    resolve: { package: HomeResolver }
  },
];

What happens here: a spinner is shown until Angular2 bootstraps, then it gets replaced by an analogous spinner, managed by Angular DOM (but rebooting spin animation, as the DOM is modified), then the HomeComponent kicks in when the HomeResolver is.. resolved.

This is sub-par experience, as the spinner resets at application bootstrap, but before the route resolve is done.

Community
  • 1
  • 1
Valerio
  • 2,390
  • 3
  • 24
  • 34
  • I think it might be impossible to do until [ works on App Root Component](https://github.com/angular/angular/issues/4946) – Valerio Mar 19 '17 at 18:26
  • I didn't quite understand but are you looking for something like this please click on the link and click any league and see the loading https://rahulrsingh09.github.io/footballdetails/ – Rahul Singh Mar 19 '17 at 18:48
  • For example in your link I can see 3 states: 1 - just 'loading...' 2 - 'Football data' header only, without data 3 - The entire page with data. What I want to accomplish is to just have 2 states => 1 - 'loading...' 2 - Home page with data (already) received and compiled – Valerio Mar 19 '17 at 20:27
  • what you want to achieve is state management in Angular for that you need to use redux in your application. Where only a single state.will be maintained. For redux you can check this repo by me https://github.com/rahulrsingh09/AngularConcepts – Rahul Singh Mar 20 '17 at 11:35
  • But for this example i can say that that you can make use of router resolve to fetch data and by default you can have a loading component – Rahul Singh Mar 20 '17 at 11:36
  • Sorry but you are out of scope, in my application I am using ngrx for state management, but this is not a state management problem, this is a DOM-redraw problematic. – Valerio Mar 20 '17 at 15:28
  • 1
    but accrodin to my last comment cant you proceed like that – Rahul Singh Mar 21 '17 at 08:18
  • Was helpfull to me. Nice link! – Pascal May 04 '17 at 21:47

0 Answers0