Angular2 component shows slim loading bar at the top of the page
npm install ng2-slim-progress-barsh
npm install ng2-slim-progress-bar --save
`
Demo
Simple examples using ng2-slim-progress-bar:
- with SystemJS in ng2-systemjs-demo
- with Webpack in ng2-webpack-demo
Online demo available here
Usage
If you use SystemJS to load your files, you might have to update your config:
`js
System.config({
map: {
'ng2-slim-progress-bar': 'node_modules/ng2-slim-progress-bar/bundles/index.umd.js'
}
});
`
#### 1. Update the markup
- Import the style.css into your web page
- Add tag in template of your application component.
#### 2. Import the SlimLoadingBarModule
Import SlimLoadingBarModule.forRoot() in the NgModule of your application.
The forRoot method is a convention for modules that provide a singleton service.
`ts
import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from '@angular/core';
import {SlimLoadingBarModule} from 'ng2-slim-progress-bar';
@NgModule({
imports: [
BrowserModule,
SlimLoadingBarModule.forRoot()
],
bootstrap: [AppComponent]
})
export class AppModule {
}
`
If you have multiple NgModules and you use one as a shared NgModule (that you import in all of your other NgModules),
don't forget that you can use it to export the SlimLoadingBarModule that you imported in order to avoid having to import it multiple times.
`ts
@NgModule({
imports: [
BrowserModule,
SlimLoadingBarModule.forRoot()
],
exports: [BrowserModule, SlimLoadingBarModule],
})
export class SharedModule {
}
`
#### 3. Use the SlimLoadingBarService for your application
- Import SlimLoadingBarService from ng2-slim-progress-bar in your application code:
`js
import {Component} from '@angular/core';
import {SlimLoadingBarService} from 'ng2-slim-progress-bar';
@Component({
selector: 'app',
template:
})
export class AppComponent {
constructor(private slimLoadingBarService: SlimLoadingBarService) { }
startLoading() {
this.slimLoadingBarService.start(() => {
console.log('Loading complete');
});
}
stopLoading() {
this.slimLoadingBarService.stop();
}
completeLoading() {
this.slimLoadingBarService.complete();
}
}
`
#### 3. Customize the the ng2-slim-progress-bar for your application
You can use the following properties to customize the ng2-slim-progress-bar component in your template:
- color - The color of loading bar. Default is firebrick. Any CSS compatible value.
- height - The height of loading bar. Default value is 2px.
- show - The flag helps hide and show the loading bar. Devault value is true.
Example:
#### 4. Manage the loading bar
You can use the following properties to customize the SlimLoadingBar via instance of SlimLoadingBarService:
- color - The color of loading bar.
- height - The height of loading bar.
- visible - The flag helps hide and show the loading bar.
You can use the following methods to control the SlimLoadingBar via instance of SlimLoadingBarService:
- start - Start the loading progress. Use the callback function as an parameter to listed the complete event.
- startIncrease - Start the loading progress with automatically slow down the increment. Use the callback function as an parameter to listed the complete event or it will stop on 99%.
- stop - Stop the loading progress. This method pause the current position of loading progress.
- reset- Reset the position of loading progress to 0.
- complete` - Set the progress to 100% and hide the progress bar.