【发布时间】:2020-09-19 12:25:51
【问题描述】:
我正在尝试为月/年创建导航。
如果用户在Jan 月份,我想禁用left 箭头导航按钮,如果用户在Dec 月份,我想禁用右导航按钮。
下面是我的代码
ngOnInit() {
this.year = new Date().getFullYear();
this.monthIndex = new Date().getMonth();
}
浏览月份
- 如果“flag”为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-->
但我的代码没有按预期工作,
我在这里做错了什么?
【问题讨论】:
-
您在哪里使用此代码添加模板代码以获得更好的想法。
-
更新了我的问题
-
monthNavigatorValidation(flag)正在返回一个布尔值,尽量不要返回真或假值,尤其是您使用isLeftMonthDisabled和isRightMonthDisabled禁用按钮
标签: javascript angular typescript