Angular 4 countdown is compatible with latest release of angular 4.x.x. Click here to open Demo App.
Follow these steps:
app.module.ts
file.import CountDownModule in your app.module.ts file
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { CountDownModule } from "ngx-countdown"; <-----
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
CountDownModule <-----
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
adding CountDownModule in NgModule make it accessible in whole project.
<app-count-down [time]="'10'" [object]="'2'" [singleFormat]="'hh:mm:ss'"></app-count-down>
add above selector to any component.html file, will pops-up countdown of 10 seconds. You can customize it further like this below
<app-count-down [time]="'15'" [object]="'2'" [singleFormat]="'hh-mm-ss'" (onStart)="onStart($event)" (onComplete)="onComplete($event)" [selectedtheme]="'material'"></app-count-down>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
onStart(data){
console.log(data);
}
onComplete(data){
console.log(data);
}
}
Above Code will pops-up countdown of 15 seconds with 'hh-mm-ss' format, and material theme. It will call onStart method when timer starts and calls onComplete method when timer ends.
You can add multiple timers in single component like this
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
onStart1(){
console.log("Countdown 1 starts");
}
onComplete1(){
console.log("Countdown 1 ends");
}
onStart2(){
console.log("Countdown 2 starts");
}
onComplete2(){
console.log("Countdown 2 ends");
}
}
<app-count-down [time]="'10'" [singleFormat]="'hh-mm-ss'" (onStart)="onStart1()" (onComplete)="onComplete1()" [selectedtheme]="'material'"></app-count-down>
<app-count-down [time]="'20'" [singleFormat]="'hh:mm:ss'" (onStart)="onStart2()" (onComplete)="onComplete2()" [selectedtheme]="'default'"></app-count-down>
Above code will show two countdowns. First with 10 seconds and second with 20 seconds. You could define onStart and onComplete events on every countdown element, and it will run as well when counter starts or completes.