npm i ngx-loading
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { NgxLoadingModule } from 'ngx-loading'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, NgxLoadingModule.forRoot({}), ], providers: [], bootstrap: [AppComponent], }) export class AppModule {} |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { loading = false; // Optional configuration for the loading spinner loadingConfig = { primaryColour: '#007bff', secondaryColour: '#6c757d', tertiaryColour: '#d1ecf1', }; showLoading() { this.loading = true; setTimeout(() => { this.loading = false; // Automatically hide after 3 seconds }, 3000); } hideLoading() { this.loading = false; } } |
app.component.html
1 2 3 4 5 6 7 |
<div> <button (click)="showLoading()">Show Loading</button> <button (click)="hideLoading()">Hide Loading</button> </div> <!-- Loading spinner --> <ngx-loading [show]="loading" [config]="loadingConfig"></ngx-loading> |
FULL SOURCE CODE