亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

angular2倒計(jì)時(shí)組件使用詳解

 更新時(shí)間:2017年01月12日 14:12:07   作者:weixin_36333953  
這篇文章主要為大家詳細(xì)介紹了angular2倒計(jì)時(shí)組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

項(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論