How to display loading screen when navigating between routes using Angular?
Last Updated :
12 Mar, 2021
In this post, we will see how to display a loading screen when navigating from one component to another. When the user navigates through routes, the app may communicate with the backend to load some useful data, and it may produce some delay. At that time, if the user doesn't see anything on the screen, he may think that either the app is broken or something is wrong with the client machine. Hence, it is necessary to keep the user engaged with the app with the help of some message or loading animation.
Prerequisites: NPM must be installed
Environment Setup: We will create a simple app that will simulate some delays while navigation and show a loading spinner while navigating through routes. Let's quickly set up the environment:
npm install -g @angular/cli
ng new <project-name>
Project Structure: After executing the above commands, you will get a project structure like this:
Project Structure
Now execute these commands:
cd <project-name>
ng serve -o
Output: Open http://localhost:4200 and check whether the default angular landing page is loading or not.
Follow the below steps:
Step 1: We will be loading data from a JSON file instead of an actual server. In src/assets/ create a new file data.json and add the following data.
data.json:
{
"K.S. Williamson": 0,
"S.P.D. Smith": 0,
"M. Labuschagne": 0,
"J.E. Root": 0,
"V. Kohli": 0,
"Babar Azam": 0,
"H.M. Nicholls": 0,
"C.A. Pujara": 0,
"D.A. Warner": 0,
"B.A. Stokes": 0,
"Gerard Abood": 1,
"Afzaal Ahmed": 1,
"Mesbahuddin Ahmed": 1,
"Tanvir Ahmed": 1,
"Javed Akhtar": 1,
"A. F. M. Akhtaruddin": 1,
"Rizwan Akram": 1,
"Jahangir Alam": 1,
"Brian Aldridge": 1
}
Step 2: To read this data we will use HttpClient module of angular. We need to register it in the file app.module.ts.
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
// Import this module
import {HttpClientModule} from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule // Register the module here
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 3: Now create two new components. We will navigate between these two.
ng generate component batsman
ng generate component umpire
It will generate 4 files for each component. We will understand the code of one of these. The other one will have similar working. In batsman.component.ts, add the following code.
batsman.component.ts
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-batsman',
templateUrl: './batsman.component.html',
styleUrls: ['./batsman.component.css']
})
export class BatsmanComponent implements OnInit {
constructor(private http:HttpClient) { }
batsman = []
ngOnInit(): void {
this.http.get('assets/data.json').subscribe(
result => {
setTimeout(() => {
for (let key in result) {
if (!result[key]) {
this.batsman.push(key);
}
}
}, 2000);
}
)
}
}
First, import the HttpClient class. We have created an HttpClient object in the constructor as a dependency injection. We have also initialized an empty batsman array. The get() method of HttpClient will return an Observable which returns the data as the first parameter in its subscribe(result_callback, error_callback) method. In the callback, we have simulated a 2000 ms delay and pushed the names with 0 value. This means that as soon as the page is loaded, there will be a 2 seconds delay and the batsman array will be populated. Now add the following code to batsman.component.html.
batsman.component.html
<div *ngIf="!batsman.length">
<div class="spinner-border m-5" role="status">
<span class="sr-only"></span>
</div>
</div>
<div *ngIf="batsman.length">
<table>
<tr *ngFor="let person of batsman">
<td>{{person}}</td>
</tr>
</table>
</div>
Step 4: There are two div tags. The first one is displayed when the batsman array is empty. The other one will be displayed when the array is populated. So until the data is not received, we will be viewing the first div which contains the loading animation. The spinner-border class exists in Bootstrap. So we need to add bootstrap to our project. Edit the index.html file as below.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Geeks For Geeks</title>
<base href="/" />
<meta name="viewport"
content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon"
href="favicon.ico" />
<!--Add this line-->
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity=
"sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
crossorigin="anonymous"/>
</head>
<body>
<app-root></app-root>
<!--Add these lines-->
<script src=
"https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity=
"sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi"
crossorigin="anonymous">
</script>
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity=
"sha384-nsg8ua9HAw1y0W1btsyWgBklPnCUAFLuTMS2G72MMONqmOymq585AcH49TLBQObG"
crossorigin="anonymous">
</script>
</body>
</html>
Step 5: Now add the following codes to umpires.component.ts and umpires.component.html respectively.
umpires.component.ts
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { CricketService } from '../cricket.service';
@Component({
selector: 'app-umpires',
templateUrl: './umpires.component.html',
styleUrls: ['./umpires.component.css']
})
export class UmpiresComponent implements OnInit {
constructor(private http:HttpClient) { }
umpires = [];
ngOnInit(): void {
this.http.get('assets/data.json').subscribe(
result => {
setTimeout(() => {
for (let key in result) {
if (result[key]) {
this.umpires.push(key);
}
}
}, 2000);
}
)
}
}
umpires.component.html
<div *ngIf="!umpires.length">
<div class="spinner-border m-5" role="status">
<span class="sr-only"></span>
</div>
</div>
<div *ngIf="umpires.length">
<table>
<tr *ngFor="let person of umpires">
<td>{{person}}</td>
</tr>
</table>
</div>
Step 6: Create routes for these components in app-routing.module.ts as below:
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { BatsmanComponent } from './batsman/batsman.component';
import { UmpiresComponent } from './umpires/umpires.component';
const routes: Routes = [
{path:'batsman', component:BatsmanComponent},
{path:'umpires', component:UmpiresComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Step 7: Add the path in 'path' key and respective component in 'component' key. Import the necessary Components. Now create the links for the user in app.component.html and the coding part is finished:
app.component.html
<div>
<a [routerLink]="['batsman']">Batsman</a> ||
<a [routerLink]="['umpires']">Umpires</a>
<router-outlet></router-outlet>
</div>
The above code creates two links that navigate to the respective components. The <router-outlet> tag displays the navigated component.
Run the below command:
ng serve -o
Output:
Similar Reads
Angular Interview Questions and Answers Angular is a popular framework for building dynamic web applications. Developed and maintained by Google, Angular allows developers to create fast, efficient, and scalable single-page applications (SPAs) that provide a seamless user experience. Google, Accenture, Microsoft, PayPal, Upwork, Netflix,
15+ min read
Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
Angular Tutorial Angular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
AngularJS Tutorial AngularJS is a free and open-source JavaScript framework that helps developers build modern web applications. It extends HTML with new attributes and it is perfect for single-page applications (SPAs). AngularJS, developed by Google, has been important in web development since its inception in 2009.
5 min read
Introduction to AngularJS AngularJS is a popular open-source framework that simplifies web development by creating interactive single-page applications (SPAs). Unlike traditional websites that load new pages for each click, SPAs offer a smoother user experience by updating content on the same page. AngularJS makes this possi
4 min read
Angular CLI | Angular Project Setup Angular is an open-source front-end web application framework that is used for building single-page and complex web applications. By default, angular uses TypeScript for creating logic but as the browser doesn't know typescript it converts typescript into javascript in order to make typescript under
3 min read
Explain the Architecture Overview of Angular ? Angular is a client-side front-end framework developed by a team of developers at Google based on Typescript. It is used for building dynamic and single-page web applications (SPAs). Also, Angular is a versatile framework for building web applications and offers a wide range of features and tools to
6 min read
Angular Cheat Sheet - A Basic Guide to Angular Angular is a client-side TypeScript-based, front-end web framework developed by the Angular Team at Google, that is mainly used to develop scalable single-page web applications(SPAs) for mobile & desktop. Angular is a great, reusable UI (User Interface) library for developers that helps in build
15+ min read
Component Lifecycle in Angular In Angular, Components are the fundamental building blocks of an application. Understanding the lifecycle of these components is crucial for effective Angular Development. Angular provides several lifecycle hooks that allow developers to tap into key moments in a Componentâs lifecycle and execute cu
3 min read
Difference Between React.js and Angular.js When it comes to building modern web applications, React.js and Angular.js are two of the most popular choices. Both are used in front-end development, but they differ significantly in terms of architecture, features, and usage.What is React.js?React.js is a declarative, efficient, and flexible Java
5 min read