如何使用Angular创建 "当前日期 "按钮?

问题描述 投票:0回答:1

我目前正在建立一个日程安排应用,这个应用有前一天、当前一天和第二天的按钮。我如何创建一个当日按钮?

HTML文件

    <button type="button" (click)="previousDay()" >Left</button> 
    <br>
    <h2>{{ currentDay }}</h2>
    <br>
    <button type="button" (click)="nextDay()" >Right</button>
    <br>
    <button type="button" (click)="currentDay()" >Current Day</button>
    <br>
</div>

TypeScript文件。

export class DayViewComponent implements OnInit {

  events: any[];
  currentDay: Date;
  modalActive: boolean = false;

  previousDay() {
    this.currentDay.setDate(this.currentDay.getDate()-1);
  }

  nextDay() {
    this.currentDay.setDate(this.currentDay.getDate()+1);
  } 

  currentDay(){

}
}
angular typescript button
1个回答
0
投票

我对你的函数的命名很谨慎。命名为 currentDay 约会 一个函数。在你的函数前加上'set'怎么样?

    setCurrentDay() {
      this.currentDay = new Date()
    }
© www.soinside.com 2019 - 2024. All rights reserved.