angular2倒計時組件使用詳解
項目中遇到倒計時需求,考慮到以后在其他模塊也會用到,就自己封裝了一個組件。便于以后復(fù)用。
組件需求如下:
- 接收父級組件傳遞截止日期
- 接收父級組件傳遞標題
組件效果
變量
組件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"> 小時 </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; // 父組件傳遞標題 @Input() title: string; // 小時差 private hour: number; // 分鐘差 private minute: number; // 秒數(shù)差 private second: number; // 時間差 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; } // 定時器 private timer; // 每一秒更新時間差 ngAfterViewInit() { this.timer = setInterval(() => { this.diff = this.endDate - Date.now(); }, 1000); } // 銷毀組件時清除定時器 ngOnDestroy() { if (this.timer) { clearInterval(this.timer); } } }
使用方法demo.html
<roy-countdown title="距離考試還有:" [endDate]="endDate"></roy-countdown>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Angular 4.x 動態(tài)創(chuàng)建組件
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- Angular2學(xué)習(xí)教程之組件中的DOM操作詳解
- angular中不同的組件間傳值與通信的方法
- Angular父組件調(diào)用子組件的方法
- Angular5給組件本身的標簽添加樣式class的方法
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
- 詳解angular2封裝material2對話框組件
- 簡單談?wù)凙ngular中的獨立組件的使用
相關(guān)文章
探討AngularJs中ui.route的簡單應(yīng)用
這篇文章主要介紹了AngularJs中ui.route的簡單應(yīng)用,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11解決angular2在雙向數(shù)據(jù)綁定時[(ngModel)]無法使用的問題
今天小編就為大家分享一篇解決angular2在雙向數(shù)據(jù)綁定時[(ngModel)]無法使用的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09AngularJS動態(tài)綁定ng-options的ng-model實例代碼
本篇文章主要介紹了AngularJS動態(tài)綁定ng-options的ng-model實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
這篇文章主要介紹了JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定,包括作用域的繼承以及數(shù)據(jù)的單向和雙向綁定等重要知識點,需要的朋友可以參考下2016-03-03Angularjs material 實現(xiàn)搜索框功能
這篇文章主要介紹了Angularjs material 實現(xiàn)搜索框功能的相關(guān)資料,需要的朋友可以參考下2016-03-03angularJs-$http實現(xiàn)百度搜索時的動態(tài)下拉框示例
下面小編就為大家分享一篇angularJs-$http實現(xiàn)百度搜索時的動態(tài)下拉框示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02