html - Show loading screen when navigating between routes in Angular 2

Html - Show loading screen when navigating between routes in Angular 2

To show a loading screen when navigating between routes in an Angular application, you can leverage Angular's Router events and create a service or component to manage the loading state. Here's a step-by-step guide on how to achieve this:

Step 1: Create a Loading Service

First, create a service to manage the loading state across your application. This service will track when navigation starts and ends.

  1. Generate the Service

    Run the following Angular CLI command to generate a service:

    ng generate service loading 
  2. Implement the Loading Service

    Open loading.service.ts and implement the service:

    import { Injectable } from '@angular/core'; import { NavigationStart, Router } from '@angular/router'; import { Observable, Subject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class LoadingService { private loadingSubject = new Subject<boolean>(); loading$: Observable<boolean> = this.loadingSubject.asObservable(); constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loadingSubject.next(true); } else { this.loadingSubject.next(false); } }); } } 
    • loadingSubject: A Subject to emit loading state changes.
    • loading$: An observable that components can subscribe to for loading state changes.
    • Subscribes to router.events to detect when navigation starts (NavigationStart) and ends (NavigationEnd, NavigationCancel, NavigationError).

Step 2: Create a Loading Component (Optional)

You can create a loading component to display a loading indicator or screen.

  1. Generate the Component

    Run the following Angular CLI command to generate a component:

    ng generate component loading 
  2. Implement the Loading Component

    Modify loading.component.html to display a loading message or spinner:

    <div *ngIf="loading" class="loading-overlay"> <div class="spinner"></div> <p>Loading...</p> </div> 

    Update loading.component.ts to subscribe to the LoadingService:

    import { Component, OnInit } from '@angular/core'; import { LoadingService } from '../loading.service'; @Component({ selector: 'app-loading', templateUrl: './loading.component.html', styleUrls: ['./loading.component.css'] }) export class LoadingComponent implements OnInit { loading: boolean; constructor(private loadingService: LoadingService) { } ngOnInit(): void { this.loadingService.loading$.subscribe(loading => { this.loading = loading; }); } } 

    Add styles in loading.component.css to style your loading overlay as needed.

Step 3: Use the Loading Component

Include the LoadingComponent in your main application template (app.component.html or your layout component) to display the loading indicator:

<app-loading></app-loading> 

Step 4: Styling (Optional)

Style your loading overlay (loading.component.css) to match your application's design and branding.

.loading-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 9999; /* Adjust as needed */ } .spinner { border: 4px solid rgba(255, 255, 255, 0.3); border-radius: 50%; border-top: 4px solid #ffffff; width: 50px; height: 50px; animation: spin 1s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } 

Step 5: Testing

Navigate between routes in your Angular application, and you should see the loading overlay displayed when navigation starts and hidden when navigation completes.

Notes

  • Ensure that the LoadingService is provided in the root (@Injectable({ providedIn: 'root' })) to maintain a single instance across your application.
  • Customize the loading indicator and styles (spinner, loading-overlay) to match your application's design.
  • Handle edge cases such as long-running operations or error scenarios in your application.

By following these steps, you can effectively implement a loading screen or indicator when navigating between routes in your Angular 2+ application, enhancing the user experience with visual feedback during navigation.

Examples

  1. Angular 2 show loading screen between routes

    • Description: How to implement a loading screen or spinner to indicate when Angular 2 routes are being navigated.
    • Code:
      // app.component.ts import { Component } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { loading: boolean = false; constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loading = true; } else if (event instanceof NavigationEnd) { this.loading = false; } }); } } 
      <!-- app.component.html --> <div *ngIf="loading" class="loading-screen"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 
  2. Angular 2 spinner between routes

    • Description: How to add a spinner specifically to show loading activity between Angular 2 routes.
    • Code:
      <!-- app.component.html --> <div *ngIf="loading" class="spinner-container"> <div class="spinner"></div> </div> <router-outlet></router-outlet> 
      /* styles.css or component-specific CSS */ .spinner-container { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 9999; } .spinner { border: 4px solid rgba(255, 255, 255, 0.3); border-radius: 50%; border-top: 4px solid #ffffff; width: 40px; height: 40px; animation: spin 1s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } 
  3. Angular 2 loading indicator between route changes

    • Description: How to show a loading indicator whenever a route change is initiated in an Angular 2 application.
    • Code:
      // loading-indicator.service.ts import { Injectable } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; import { BehaviorSubject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class LoadingIndicatorService { public isLoading: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.isLoading.next(true); } else if (event instanceof NavigationEnd) { this.isLoading.next(false); } }); } } 
      <!-- app.component.html --> <div *ngIf="loadingIndicatorService.isLoading | async" class="loading-indicator"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 
  4. Angular 2 router loading screen

    • Description: How to integrate a loading screen that appears during Angular 2 router transitions.
    • Code:
      // app.component.ts import { Component } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { loading: boolean = false; constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loading = true; } else if (event instanceof NavigationEnd) { this.loading = false; } }); } } 
      <!-- app.component.html --> <div *ngIf="loading" class="loading-screen"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 
  5. Angular 2 show loading indicator on route change

    • Description: How to display a loading indicator or spinner specifically during Angular 2 route transitions.
    • Code:
      // app.component.ts import { Component } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { loading: boolean = false; constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loading = true; } else if (event instanceof NavigationEnd) { this.loading = false; } }); } } 
      <!-- app.component.html --> <div *ngIf="loading" class="loading-screen"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 
  6. Angular 2 loading spinner between routes

    • Description: How to implement a loading spinner specifically for Angular 2 route changes.
    • Code:
      <!-- app.component.html --> <div *ngIf="loading" class="spinner-container"> <div class="spinner"></div> </div> <router-outlet></router-outlet> 
      /* styles.css or component-specific CSS */ .spinner-container { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 9999; } .spinner { border: 4px solid rgba(255, 255, 255, 0.3); border-radius: 50%; border-top: 4px solid #ffffff; width: 40px; height: 40px; animation: spin 1s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } 
  7. Angular 2 route change loading screen

    • Description: How to display a loading screen or spinner during Angular 2 route transitions.
    • Code:
      // app.component.ts import { Component } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { loading: boolean = false; constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loading = true; } else if (event instanceof NavigationEnd) { this.loading = false; } }); } } 
      <!-- app.component.html --> <div *ngIf="loading" class="loading-screen"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 
  8. Angular 2 loading screen on route change

    • Description: How to add a loading screen or spinner that appears during Angular 2 route transitions.
    • Code:
      // app.component.ts import { Component } from '@angular/core'; import { Router, NavigationStart, NavigationEnd } from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { loading: boolean = false; constructor(private router: Router) { this.router.events.subscribe(event => { if (event instanceof NavigationStart) { this.loading = true; } else if (event instanceof NavigationEnd) { this.loading = false; } }); } } 
      <!-- app.component.html --> <div *ngIf="loading" class="loading-screen"> <div class="spinner"></div> <p>Loading...</p> </div> <router-outlet></router-outlet> 

More Tags

onpress bson format-conversion export-to-excel android-thread rabbitmq-exchange apache-commons-httpclient xampp qsqlquery repository-pattern

More Programming Questions

More Entertainment Anecdotes Calculators

More Date and Time Calculators

More Financial Calculators

More Biochemistry Calculators