Open In App

How to add HttpClient Progressive Bar to an Angular Application ?

In this article, we will build a web application in Angular where we will add an HttpClient Progressive Bar to an Angular Application. Here, we’ll add Bootstrap for styling, angular material for material design component and then we will add a progressive bar for every HTTP calls.

Prerequisites: Before we start, you need to install and configure the tools below to create the Angular application:



Steps to Add Progressive Bar to an Angular Application: The following steps will be implemented to include the HttpClient Progressive Bar in an Angular App:

 



Step 1: Create an Angular application

> ng new appwithprogressivebar
? Would you like to add Angular routing? Yes
? Which stylesheet format would you like to use? CSS
. . .
CREATE appwithprogressivebar/src/app/app.component.css (0 bytes)
✔ Packages installed successfully.
    Successfully initialized git.
> ng add bootstrap
> ng add @angular/material
"styles": [
    "@angular/material/prebuilt-themes/indigo-pink.css",
    "src/styles.css",
    "./node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
    "./node_modules/bootstrap/dist/js/bootstrap.js"
]

Step 2: Add Loader Service 

ng g s services/loader




import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
  
@Injectable({
    providedIn: 'root'
})
export class LoaderService {
    isLoading = new Subject < boolean > ();
  
    constructor() { }
    show() {
        this.isLoading.next(true);
    }
    hide() {
        this.isLoading.next(false);
    }
}

Step 3:  Add Interceptor

ng g interceptor interceptor/loader




import { Injectable } from '@angular/core';
import {
    HttpRequest,
    HttpHandler,
    HttpEvent,
    HttpInterceptor
} from '@angular/common/http';
import { finalize, Observable } from 'rxjs';
import { LoaderService } from '../services/loader.service';
  
@Injectable()
export class LoaderInterceptor implements HttpInterceptor {
  
    constructor(public loaderService: LoaderService) { }
  
    intercept(request: HttpRequest<unknown>, next: HttpHandler):
        Observable<HttpEvent<unknown>> {
        this.loaderService.show();
        return next.handle(request).pipe(
            finalize(() => this.loaderService.hide())
        )
    }
}

Step 4:  Add Services and Interceptor 

providers: [
    LoaderService,
    { provide: HTTP_INTERCEPTORS, useClass: LoaderInterceptor, multi: true}
  ]
import { LoaderService } from './services/loader.service';
import { LoaderInterceptor } from './interceptor/loader.interceptor';
import { HTTP_INTERCEPTORS } from '@angular/common/http';

Step 5: Add Progressive Bar 

imports: [
    ...,
    MatProgressBarModule
  ],
import {MatProgressBarModule} from '@angular/material/progress-bar';
> ng g c loader




import { Component } from '@angular/core';
import { LoaderService } \
    from '../services/loader.service';
  
@Component({
    selector: 'app-loader',
    templateUrl: './loader.component.html',
    styleUrls: ['./loader.component.css']
})
export class LoaderComponent {
    loading: boolean = false;
  
    constructor(private loaderService: LoaderService) {
        this.loaderService.isLoading.subscribe((v) => {
            this.loading = v;
        });
    }
}

<mat-progress-bar *ngIf="loading" mode="indeterminate"></mat-progress-bar>
<app-loader></app-loader>

Step 6:  Add HTTP call for testing 

imports: [
    ...,
    HttpClientModule
  ],
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';




import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'appwithprogressivebar';
  
    constructor(private http: HttpClient) {
    }
  
    makeHttpCall() {
        this.http.get(
"https://...com/posts/?_delay=2000").subscribe(d => {
                console.log(d);
            })
    }
}




<app-loader></app-loader>
<div class="container d-flex 
            align-items-center 
            justify-content-center" 
     style="height: 90vh;">
    <button type="button" 
            (click)="makeHttpCall()" 
            class="btn btn-primary">
            Make Http Call
    </button>
</div>

Screen

Step 7: Run the application

> ng serve

✔ Browser application bundle generation complete.
. . .
** Angular Live Development Server is listening on localhost:4200, 
   open your browser on http://localhost:4200/ **

√ Compiled successfully.

Output:

Screen with progressive bar


Article Tags :