当ion-checkbox设置为true时,如何添加添加设备时间戳

时间:2016-08-05 20:49:04

标签: ionic2

我目前正在使用复选框来跟踪我的离子2应用程序中的任务完成时间。选中此复选框后,是否可以通过移动设备保存日期/时间。

<ion-list>
  <ion-item>
    <ion-label>Task 1</ion-label>
    <ion-checkbox [(ngModel)]="data.task1"></ion-checkbox>
  </ion-item>    
</ion-list>

1 个答案:

答案 0 :(得分:1)

您可以使用Javascript中的Date object来执行此操作,就像您在this plunker中看到的一样。

您的Component

import { Component } from "@angular/core";

@Component({
     templateUrl:"home.html"
})
export class HomePage {

  public data: any;
  public dateTime : string = '';

  constructor() {
    this.data = {
      task1 : false
    }
  }

  public changeCheckBox() {
    if(this.data.task1) {
      this.dateTime = new Date();
    }
  }
}

您的观点:

<ion-content>
  <ion-list>
    <ion-item>
      <ion-label>Task 1</ion-label>
      <ion-checkbox [(ngModel)]="data.task1" (ionChange)="changeCheckBox()"></ion-checkbox>
    </ion-item>  
    <ion-item>
      <ion-label>Timestamp: {{ dateTime }}</ion-label>
    </ion-item>
  </ion-list>
</ion-content>

当然,您可以使用一些get方法获取详细信息:

Date.prototype.getDate()
Date.prototype.getDay()
Date.prototype.getFullYear()
Date.prototype.getHours()
Date.prototype.getMilliseconds()
Date.prototype.getMinutes()
Date.prototype.getMonth()
Date.prototype.getSeconds()
Date.prototype.getTime()
Date.prototype.getTimezoneOffset()
Date.prototype.getUTCDate()
Date.prototype.getUTCDay()
Date.prototype.getUTCFullYear()
Date.prototype.getUTCHours()
Date.prototype.getUTCMilliseconds()
Date.prototype.getUTCMinutes()
Date.prototype.getUTCMonth()
Date.prototype.getUTCSeconds()
Date.prototype.getYear()