@ngx-loading-bar
A fully automatic loading bar with zero configuration for angular app (http, http-client and router).
Packages
Demo
Quick Start
1. Install one or all @ngx-loading-bar libraries:
npm install @ngx-loading-bar/http-client --save
npm install @ngx-loading-bar/http --save
npm install @ngx-loading-bar/router --save
npm install @ngx-loading-bar/core --save
2. Import the installed libraries (LoadingBarHttpClientModule
, LoadingBarHttpModule
, LoadingBarRouterModule
or LoadingBarModule
):
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { LoadingBarHttpClientModule } from '@ngx-loading-bar/http-client';
import { AppComponent } from './app';
@NgModule({
...
imports: [
...
LoadingBarHttpClientModule
],
})
export class AppModule {}
3. Include ngx-loading-bar
in your app component:
import { Component } from '@angular/core';
@Component({
selector: 'app',
template: `
...
<ngx-loading-bar></ngx-loading-bar>
`,
})
export class AppComponent {}
4. include the supplied CSS file (or create your own).
Manually manage loading service
1. Import the LoadingBarModule
import { NgModule } from '@angular/core';
import { LoadingBarModule } from '@ngx-loading-bar/core';
@NgModule({
...
imports: [
...
LoadingBarModule.forRoot(),
],
})
export class AppModule {}
2. Inject/Use LoadingBarService
import { Component } from '@angular/core';
import { LoadingBarService } from '@ngx-loading-bar/core';
@Component({
selector: 'app',
template: `
...
<ngx-loading-bar></ngx-loading-bar>
<button (click)="startLoading()">start</button>
<button (click)="stopLoading()">stop</button>
`,
})
export class App {
constructor(private loadingBar: LoadingBarService) {}
startLoading() {
this.loadingBar.start();
}
stopLoading() {
this.loadingBar.complete();
}
}
Advanced
When you import LoadingBarHttpModule, http service observables become hot. That means that a HTTP request
is sent as soon as a call to http.get (for example) has been made.
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'ng-loading-bar-app',
templateUrl: './app.html',
})
export class App {
private request$;
constructor(private _http: Http) {
this.request$ = _http.get('/app/heroes');
}
startLoadingBarHttpRequest() {
if (false) {
this.request$.subscribe();
}
}
}
This behavior is because the Loading bar module overrides default http service by setting up a subscription to the request.
This subscription fires up the HTTP request.
If this behavior doesn't suit you, you should manage loading bar manually as in the component startHttpRequest above.
Credits