How To Load A Spinner Till Data Loads In To Ui In Angular 5
Introduction
In this article, we will learn how nosotros tin show the Loader in Angular eight 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 bear witness the loading country of data in awarding.
Prerequisites
- Basic Noesis of Angular 2 or higher
- Visual Studio Lawmaking
- Visual studio and SQL Server Direction studio
- Node and NPM installed
- Bootstrap
Pace i
Create an Athwart project past using the following command.
- ng new Angularloader
Footstep 2
Open this projection in Visual Studio Code and install bootstrap by using following command
- npm install bootstrap --salve
At present open styles.css file and add Bootstrap file reference. To add reference in styles.css file add together this line.
- @import '~bootstrap/dist/css/bootstrap.min.css' ;
Step 3
At present install ngx spinner library in this projection, to install ngx spinner library use the following command
- npm install ngx-spinner --save
Footstep 4
Now Update app.module.ts
Open app.module.ts file and add together 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]
- })
- exportcourse AppModule { }
Step 5
Now create a new component past using the post-obit command.
- ng one thousand c Employeedetails --spec= imitation
Now open up employeedetails.component.ts file and import NgxSpinnerService
- import { Component, OnInit } from '@athwart/cadre' ;
- 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' ]
- })
- exportcourse EmployeedetailsComponent implements OnInit {
- emp: any;
- constructor(private employeeService: EmployeeService,
- private SpinnerService: NgxSpinnerService) { }
- ngOnInit() {
- this .GetemployeeDetails();
- }
- GetemployeeDetails() {
- this .SpinnerService.bear witness();
- this .employeeService.GetemployeeDetails().subscribe((data: any) => {
- this .emp = data;
- console.log(this .emp);
- this .SpinnerService.hide();
- });
- }
- }
Open up employeedetails.component.html file and following lines
- <div>
- <divclass = "row" mode= "margin-top:10px;margin-bottom: 10px;" >
- <divclass = "col-sm-12 btn btn-success" >
- Loader in Athwart Application with Dynamic Data
- </div>
- </div>
- <divclass = "row" style= "margin-top:10px;margin-bottom: 10px;" >
- </div>
- </div>
- <hr style="groundwork-color:blackness" />
- <div>
- <tabular arrayclass = "table table-dark table-striped" >
- <thead>
- <tr>
- <th>Id</thursday>
- <th>Proper name</th>
- <th>Age</th>
- <thursday>Address</th>
- <th>City</th>
- <th>ContactNum</th>
- <th>Salary</th>
- <th>Section</th>
- </tr>
- </thead>
- <tbody>
- <tr *ngFor="let e of emp" >
- <td>{{eastward.Id}}</td>
- <td>{{due east.Proper name}}</td>
- <td>{{e.Age}}</td>
- <td>{{eastward.Address}}</td>
- <td>{{e.City}}</td>
- <td>{{e.ContactNum}}</td>
- <td>{{eastward.Bacon}}</td>
- <td>{{due east.Department}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- <ngx-spinner bdColor="rgba(51, 51, 51, 0.8)" size= "default" type= "ball-spin-clockwise" >
- <p manner="color: white" >Please Wait. </p>
- </ngx-spinner>
Step 6
Create a new class using following control,
- ng g grade Employe --spec= false
Now open up employe.ts file and add together the following line in this class
- consign form Employe {
- Id:number;
- Name:any;
- Age:any;
- Address:whatever;
- City:any;
- ContactNum:number;
- Salary:number;
- Department:any;
- }
Pace 7
Create a new service using following command
- ng g s Employee --spec= imitation
At present open employee.service.ts file and add following lines
- import { Injectable } from '@angular/core' ;
- import { HttpClient } from"@angular/common/http" ;
- @Injectable({
- providedIn:'root'
- })
- exportform EmployeeService {
- url:any;
- constructor(private http:HttpClient ) {
- }
- GetemployeeDetails()
- {
- this .url= 'http://localhost:56932/api/Employee/Employeedetails' ;
- return this .http.get( this .url);
- }
- }
Footstep 8 - Create database and a table
Open up SQL Server Management Studio, create a database named "Employee", and in this database, create a table. Give that tabular array a name like "employee".
- CREATE TABLE [dbo].[Employee](
- [Id] [int ] IDENTITY(1,1) NOT NULL ,
- [Proper noun ] [ varchar ](fifty) NULL ,
- [Age] [int ] Nothing ,
- [Address] [varchar ](50) NULL ,
- [City] [varchar ](50) Cypher ,
- [ContactNum] [varchar ](50) Zip ,
- [Bacon] [decimal ](18, 0) Aught ,
- [Department] [varchar ](fifty) Goose egg ,
- CONSTRAINT [PK_Employee] Main KEY Amassed
- (
- [Id]ASC
- )WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ Master ]
- )ON [ PRIMARY ]
- GO
- SET ANSI_PADDING OFF
- Go
Create a new Web API projection
Stride 9
Open Visual Studio and create a new projection.
Step x
Modify the name to LoaderDemo
Footstep 11
Choose the template equally Spider web API.
Stride 12
Right-click the Models folder from Solution Explorer and go to Add >> New Particular >> data.
Step 13
Click on the "ADO.NET Entity Data Model" pick and click "Add".
Footstep 14
Select EF Designer from the database and click the "Side by side" push button.
Step 15
Add the connection properties and select database name on the next page and click OK.
Step xvi
Bank check the "Tabular array" checkbox. The internal options will be selected by default. Now, click the "Finish" push.
Footstep 17
Correct-click on the Controllers folder and add a new controller. Proper noun it as "Employee controller" and add together the following namespace in the Employee controller.
- using LoaderDemo.Models;
Footstep eighteen
At present, add together a method to fetch information from database.
Employee controller
- using Organization;
- using System.Collections.Generic;
- using Organisation.Linq;
- using System.Cyberspace;
- using System.Net.Http;
- using Arrangement.Web.Http;
- using LoaderDemo.Models;
- namespace LoaderDemo.Controllers
- {
- [RoutePrefix("Api/Employee" )]
- public course EmployeeController : ApiController
- {
- [Route("Employeedetails" )]
- [HttpGet]
- public object Studentdetails()
- {
- EmployeeEntities DB =new EmployeeEntities();
- var emp = DB.Employees.ToList();
- return emp;
- }
- }
- }
Pace xix
At present, let's enable CORS. Go to Tools, open NuGet Parcel Manager, search for CORS, and install the "Microsoft.Asp.Net.WebApi.Cors" package. Open Webapiconfig.cs and add the following lines.
- EnableCorsAttribute cors = new EnableCorsAttribute( "*" , "*" , "*" );
- config.EnableCors(cors);
Step xx
Now run the projection and check issue
When data is load, the loader is hidden.
Summary
In this article, we learned how we add Loader in Athwart application with dynamic data load.
How To Load A Spinner Till Data Loads In To Ui In Angular 5,
Source: https://www.c-sharpcorner.com/article/how-to-add-loaderspinner-in-angular-8-application/
Posted by: wagnersubbillson.blogspot.com
0 Response to "How To Load A Spinner Till Data Loads In To Ui In Angular 5"
Post a Comment