angular2倒計(jì)時(shí)組件使用詳解
項(xiàng)目中遇到倒計(jì)時(shí)需求,考慮到以后在其他模塊也會(huì)用到,就自己封裝了一個(gè)組件。便于以后復(fù)用。
組件需求如下:
- 接收父級(jí)組件傳遞截止日期
- 接收父級(jí)組件傳遞標(biāo)題
組件效果

變量

組件countdown.html代碼
<div class="count-down">
<div class="title">
<h4>
{{title}}
</h4>
</div>
<div class="body">
<div class="content">
<div class="top">
{{hour}}
</div>
<div class="bottom">
小時(shí)
</div>
</div>
<div class="content">
<div class="top">
{{minute}}
</div>
<div class="bottom">
分鐘
</div>
</div>
<div class="content">
<div class="top">
{{second}}
</div>
<div class="bottom">
秒
</div>
</div>
</div>
</div>
組件countdown.scss代碼
.count-down{
width:100%;
height:100px;
background: rgba(0,0,0,0.5);
padding: 2px 0;
.body{
margin-top: 8px;
.content{
width:29%;
float: left;
margin: 0 2%;
.top{
font-size: 20px;;
line-height: 30px;
color: black;
background: white;
border-bottom: 2px solid black;
}
.bottom{
font-size: 14px;
line-height: 20px;
background: grey;
}
}
}
}
組件countdown.component.ts代碼
import { Component, OnInit, Input, OnDestroy, AfterViewInit } from '@angular/core';
@Component({
selector: 'roy-countdown',
templateUrl: './countdown.component.html',
styleUrls: ['./countdown.component.scss']
})
export class CountdownComponent implements AfterViewInit, OnDestroy {
// 父組件傳遞截止日期
@Input() endDate: number;
// 父組件傳遞標(biāo)題
@Input() title: string;
// 小時(shí)差
private hour: number;
// 分鐘差
private minute: number;
// 秒數(shù)差
private second: number;
// 時(shí)間差
private _diff: number;
private get diff() {
return this._diff;
}
private set diff(val) {
this._diff = Math.floor(val / 1000);
this.hour = Math.floor(this._diff / 3600);
this.minute = Math.floor((this._diff % 3600) / 60);
this.second = (this._diff % 3600) % 60;
}
// 定時(shí)器
private timer;
// 每一秒更新時(shí)間差
ngAfterViewInit() {
this.timer = setInterval(() => {
this.diff = this.endDate - Date.now();
}, 1000);
}
// 銷毀組件時(shí)清除定時(shí)器
ngOnDestroy() {
if (this.timer) {
clearInterval(this.timer);
}
}
}
使用方法demo.html
<roy-countdown title="距離考試還有:" [endDate]="endDate"></roy-countdown>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Angular 4.x 動(dòng)態(tài)創(chuàng)建組件
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
- angular中不同的組件間傳值與通信的方法
- Angular父組件調(diào)用子組件的方法
- Angular5給組件本身的標(biāo)簽添加樣式class的方法
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
- 詳解angular2封裝material2對(duì)話框組件
- 簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用
相關(guān)文章
AngularJS教程 ng-style 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-style 指令,這里對(duì)ng-style 指令做了詳細(xì)的基礎(chǔ)資料整理,并附有代碼示例,有需要的朋友可以參考下2016-08-08
詳解Angular的內(nèi)置過(guò)濾器和自定義過(guò)濾器【推薦】
在實(shí)際的開(kāi)發(fā)過(guò)程中,很多后端返回給我們的數(shù)據(jù)都是需要格式化處理的,在angular中為我們內(nèi)置提供了filter指令,可以很方便的對(duì)數(shù)據(jù)進(jìn)行處理。本文將對(duì)Angular的內(nèi)置過(guò)濾器和自定義過(guò)濾器進(jìn)行詳細(xì)介紹,下面跟著小編一起來(lái)看下吧2016-12-12
詳解angularJs中自定義directive的數(shù)據(jù)交互
這篇文章主要介紹了詳解angularJs中自定義directive的數(shù)據(jù)交互,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
AngularJS基礎(chǔ) ng-show 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-show 指令,這里對(duì)ng-show 指令的基礎(chǔ)知識(shí)做了詳細(xì)介紹,并附有代碼示例,希望能幫助學(xué)習(xí)AngularJS的同學(xué)2016-08-08
Angular中自定義Debounce Click指令防止重復(fù)點(diǎn)擊
本篇文章主要介紹了Angular中自定義Debounce Click指令詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Angular動(dòng)畫實(shí)現(xiàn)的2種方式以及添加購(gòu)物車動(dòng)畫實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Angular動(dòng)畫的2種方式以及添加購(gòu)物車動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
詳解Angular-ui-BootStrap組件的解釋以及使用
這篇文章主要介紹了詳解Angular-ui-BootStrap組件的解釋以及使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Angular4項(xiàng)目中添加i18n國(guó)際化插件ngx-translate的步驟詳解
這篇文章主要跟大家介紹了關(guān)于Angular4項(xiàng)目中添加i18n國(guó)際化插件ngx-translate的步驟,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
AngularJS入門教程之?dāng)?shù)據(jù)綁定用法示例
這篇文章主要介紹了AngularJS之?dāng)?shù)據(jù)綁定用法,結(jié)合實(shí)例形式分析了AngularJS基于內(nèi)置指令ng-model實(shí)現(xiàn)數(shù)據(jù)綁定的操作技巧,需要的朋友可以參考下2016-11-11
深入理解angular2啟動(dòng)項(xiàng)目步驟
本篇文章主要介紹了深入理解angular2啟動(dòng)步驟 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

