Angular

How To Add Loader/Spinner on Http Call In Angular

Hello Friends, In this article, we will learn how to add an http-loader in the Angular application. Many developers need to auto-show loader when we call APIs or get remote data. here we have to find the solution when we get data remotely then auto hide-show loader.

First, we need to add the http-loader NPM packages. run the following command in the terminal.

npm install ng-http-loader

Now, Add HttpClientModule and NgHttpLoaderModule in the app module use the following code:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { NgHttpLoaderModule } from 'ng-http-loader';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    NgHttpLoaderModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

 

Now, simply add loader in your app.component.html:

<ng-http-loader></ng-http-loader>

You can customize the following parameters:

Spinner backdrop:- by default visible(true/false).
Background color:- the color of the spinner.
Debounce delay:- after how many milliseconds the spinner will be visible.
Extra duration:- how many extra milliseconds should the spinner be visible.
Minimum duration:- how many minimum milliseconds should the spinner be visible.
Spinner Type:- the type of spinner
– skChasingDots
– skCubeGrid
– skDoubleBounce
– skRotatingPlane
– skSpinnerPulse
– skThreeBounce
– skWanderingCubes
– skWave

Specify the spinner type this way, add the following code in app.component.ts:

import { Component } from '@angular/core'; 
import { Spinkit } from 'ng-http-loader';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css'],
})
export class AppComponent {
    public spinkit = Spinkit;
}

Update the loader in your app.component.html

<ng-http-loader 
    [backdrop]="false"
    [backgroundColor]="'#ff0000'"
    [debounceDelay]="100"
    [extraDuration]="300"
    [minDuration]="300"
    [opacity]=".7"
    [backdropBackgroundColor]="'#FEFEFE'"
    [spinner]="spinkit.skWave">
</ng-http-loader>

Note: Performing HTTP requests with the HttpClientModule API is mandatory. Otherwise, the loader will not be fired at all.

I hope this article helps you and you will like it.

Jignesh Patel

Jignesh Patel is a Senior Full Stack .Net Developer has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET MVC, AngularJS, Angular, Nodejs, Web API, EPPlus, SQL, Entity Framework, JavaScript, Azure Web Jobs, Microsoft Graph API, etc.

Share
Published by
Jignesh Patel

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago