javascript - 如果月份无效,则启用/禁用按钮
问题描述
我正在尝试创建几个月/年的导航。
如果用户在月份,我想禁用left
箭头导航按钮,如果用户在Jan
月份,我想禁用右导航按钮Dec
。
下面是我的代码
ngOnInit() {
this.year = new Date().getFullYear();
this.monthIndex = new Date().getMonth();
}
浏览月份
- 如果“标志”为 0,则表示用户单击左箭头键 <-
- 如果 "flag" 为 1,则表示用户单击右箭头键 ->
下面是我的代码,
navigationArrowMonth(flag) {
this.monthNavigatorValidation(flag)
}
monthNavigatorValidation(flag?) {
if(flag) {
if(this.monthIndex < 0 || this.monthIndex >= 11) {
this.isRightMonthDisabled = true;
return false;
} else {
this.monthIndex = this.monthIndex + 1;
this.isRightMonthDisabled = false;
return true;
}
} else {
if(this.monthIndex < 0 || this.monthIndex <= 11) {
this.isLeftMonthDisabled = true;
return false;
} else {
this.monthIndex = this.monthIndex - 1;
this.isLeftMonthDisabled = false;
return true;
}
}
}
HTML 模板
<!--Month navigation-->
<!--
On click should call navigationArrowMonth
-->
<button [disabled]="isLeftMonthDisabled" (click)="navigationArrowMonth(0)" mat-icon-button id="leftMonthKey" aria-label="left naviage">
<mat-icon>keyboard_arrow_left
</mat-icon>
</button>
<div id="monthValue" class="nameArrage" style="width: 120px;">
<!--
Display month in Alphabets
-->
{{months[monthIndex]}}
</div>
<!--
On click should call navigationArrowMonth
disable when limit reached
-->
<button [disabled]="isRightMonthDisabled" (click)="navigationArrowMonth(1)" mat-icon-button id="rightMonthKey" aria-label="right naviage">
<mat-icon>keyboard_arrow_right
</mat-icon>
</button>
<!--Month navigation end-->
但是我的代码没有按预期工作,
我在这里做错了什么?
解决方案
这个更简单的解决方案
app.component.html
<h2>{{monthIndex}}</h2>
<div>
<button
type="button"
[disabled]="monthIndex <= 0"
(click)="onMove(-1)"
>
LEFT
</button>
<button
type="button"
[disabled]="monthIndex >= 11"
(click)="onMove(1)"
>
RIGHT
</button>
</div>
app.component.ts
year: number;
monthIndex: number;
ngOnInit() {
this.year = new Date().getFullYear();
this.monthIndex = new Date().getMonth();
}
onMove(move: number): void {
this.monthIndex += move;
}
推荐阅读
- sql - 计算列上的 SQL 分组依据 - 标识符无效
- python - Python网络服务器说“连接重置”
- windows - 使用批处理脚本将 exe 安装到特定文件夹
- azure-container-service - 限制每个部署或容器的 Log Analytics 日志记录
- python-3.x - 从另一个 defaultdict 获取值并更新原始 dict
- mysql - 将 MySQL Workbench 连接到特定数据库
- java - Spring kafka反序列化中的异常
- remote-validation - 远程验证未命中
- c# - 如何将 XML 加载到列表
- google-apps-script - 如何将文件上传添加到电子表格作为自定义菜单选项?