angular - 如何使用 Angular 创建“当前日期”按钮
问题描述
我目前正在构建一个具有前一天、当天和第二天按钮的日程安排应用程序。我将如何创建当前日期按钮?
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>
打字稿文件:
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(){
}
}
解决方案
我会小心命名你的函数。currentDay
必须是日期和函数是令人困惑的。在你的函数前面加上'set'怎么样?
setCurrentDay() {
this.currentDay = new Date()
}
推荐阅读
- python - 将前几行聚合到列表中,具体取决于单独的列
- swift - Swift 比较因转换变量而失败
- pine-script - Pinscript:获取当前每日打开
- angular - Angular - 如何从表单输入中的集合中调用对象属性
- css - 模拟翻书 vue,但没有 Javascript 和图像
- jquery - jquery UI Tabs - 在幻灯片更改时运行脚本
- ruby - Ruby Tkinter 滚动条不会出现在其框架中,并且允许滚动超出画布边界。我究竟做错了什么?
- go - 以循环方式向客户端提供消息的 WebSocket 服务器
- css - 文本左侧水平线与文本对齐中心的 CSS 技术
- slurm - 链接多个具有依赖关系的 SLURM 作业