Skip to main content

Adding Loader/Spinner In Angular 8 Application

Introduction

In a web application, we generally show a process status by graphical indicators like progress bars and spinners. In this post, we will discuss How To Add Loader/Spinner In Angular 8 Application, we will learn how we can show the Loader in Angular 8 using Ngx spinner library. Ngx spinner is a library for loading spinner for Angular, which is meant to inform the user that data loading is in progress. Basically Loader is used to show the loading state of data in application.

Prerequisites

  • Basic Knowledge of Angular 2 or higher
  • Visual Studio Code
  • Visual studio and SQL Server Management studio
  • Node and NPM installed
  • Bootstrap

Step 1

Create an Angular project by using the following command.

ng new Angularloader 

Step 2 

Open this project in Visual Studio Code and install bootstrap by using following command

npm install bootstrap --save  

Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.

@import '~bootstrap/dist/css/bootstrap.min.css';  

Step 3

Now install ngx spinner library in this project, to install ngx spinner library use the following command

npm install ngx-spinner --save  

Step 4

Now Update app.module.ts

Open app.module.ts file and add Import NgxSpinnerModule in the root module, 

import { BrowserModule } from '@angular/platform-browser';  
import { NgModule } from '@angular/core';  
import { AppRoutingModule } from './app-routing.module';  
import { AppComponent } from './app.component';  
import { HttpClientModule } from '@angular/common/http'  
import { EmployeedetailsComponent } from './employeedetails/employeedetails.component';  
import { NgxSpinnerModule } from "ngx-spinner";  
@NgModule({  
  declarations: [  
    AppComponent,  
    EmployeedetailsComponent  
  ],  
  imports: [  
    BrowserModule,  
    AppRoutingModule,  
    HttpClientModule,  
    NgxSpinnerModule  
  ],  
  providers: [],  
  bootstrap: [AppComponent]  
})  
export class AppModule { }  

This is image title

Step 5

Now create a new component by using the following command.

ng g c Employeedetails --spec=false  

Now open employeedetails.component.ts file and import NgxSpinnerService 

import { Component, OnInit } from '@angular/core';  
import { EmployeeService } from "../employee.service";  
import { Employe } from "../employe";  
import { NgxSpinnerService } from "ngx-spinner";  
@Component({  
  selector: 'app-employeedetails',  
  templateUrl: './employeedetails.component.html',  
  styleUrls: ['./employeedetails.component.css']  
})  
export class EmployeedetailsComponent implements OnInit {  
  emp: any;  
  constructor(private employeeService: EmployeeService,  
    private SpinnerService: NgxSpinnerService) { }  

  ngOnInit() {  
    this.GetemployeeDetails();  
  }  
  GetemployeeDetails() {  
    this.SpinnerService.show();  
    this.employeeService.GetemployeeDetails().subscribe((data: any) => {  
      this.emp = data;  
      console.log(this.emp);  
       this.SpinnerService.hide();  
    });  
  }  
}  

This is image title

Open employeedetails.component.html file and following lines

<div>    
    <div class="row" style="margin-top:10px;margin-bottom: 10px;">    
      <div class="col-sm-12 btn btn-success">    
        Loader in Angular Application with Dynamic  Data     
      </div>    
    </div>    
    <div class="row" style="margin-top:10px;margin-bottom: 10px;">    
    </div>    
  </div>    
  <hr style="background-color:black" />    

<div>  
    <table class="table table-dark table-striped">  
      <thead>  
        <tr>  
          <th>Id</th>  
          <th>Name</th>  
          <th>Age</th>  
          <th>Address</th>  
          <th>City</th>  
          <th>ContactNum</th>  
          <th>Salary</th>  
          <th>Department</th>  
        </tr>  
      </thead>  
      <tbody>  
        <tr *ngFor="let e of emp">  
          <td>{{e.Id}}</td>  
          <td>{{e.Name}}</td>  
          <td>{{e.Age}}</td>  
          <td>{{e.Address}}</td>  
          <td>{{e.City}}</td>  
          <td>{{e.ContactNum}}</td>  
          <td>{{e.Salary}}</td>  
          <td>{{e.Department}}</td>  
        </tr>  
      </tbody>  
    </table>  
  </div>  
  <ngx-spinner bdColor="rgba(51, 51, 51, 0.8)" size="default" type="ball-spin-clockwise">  
    <p style="color: white">Please Wait. </p>  
    </ngx-spinner>    

Step 6

Create a new class using following command,

ng g class  Employe --spec=false  

Now open employe.ts file and add the following line in this class

export class Employe {  
    Id:number;  
    Name:any;  
    Age:any;  
    Address:any;  
    City:any;  
    ContactNum:number;  
    Salary:number;  
    Department:any;  
}  

Step 7

Create a new service using following command

ng g s Employee --spec=false  

Now open employee.service.ts file and add following lines

import { Injectable } from '@angular/core';  
import { HttpClient } from "@angular/common/http";  
@Injectable({  
  providedIn: 'root'  
})  
export class EmployeeService {  
  url:any;  
  constructor(private http:HttpClient ) {   

  }  
  GetemployeeDetails()  
  {  
    this.url='http://localhost:56932/api/Employee/Employeedetails';  
    return this.http.get(this.url);  
  }  
}   

*Step 8 - Create database and a table *

Open SQL Server Management Studio, create a database named "Employee", and in this database, create a table. Give that table a name like "employee".

CREATE TABLE [dbo].[Employee](    
    [Id] [int] IDENTITY(1,1) NOT NULL,    
    [Name] [varchar](50) NULL,    
    [Age] [int] NULL,    
    [Address] [varchar](50) NULL,    
    [City] [varchar](50) NULL,    
    [ContactNum] [varchar](50) NULL,    
    [Salary] [decimal](18, 0) NULL,    
    [Department] [varchar](50) NULL,    
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED     
(    
    [Id] ASC    
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]    
) ON [PRIMARY]    

GO    

SET ANSI_PADDING OFF    
GO     

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...