提问人:Carlos López Monreal 提问时间:10/11/2023 最后编辑:Carlos López Monreal 更新时间:10/11/2023 访问量:27
在 Angular 中禁用输入日期的天数范围
Disable a range of days in input date in Angular
问:
我有一个 Angular 组件,其中有一个输入日期,我想禁用过去的所有日期,同时禁用未来的特定日期范围,例如 2023-10-15 和 2023-10-23 之间的日期。我该怎么做?这可能吗?先谢谢你。
我尝试过这样的事情:
<input type="date" appDateRange>
其中 appDateRange 是指令:
import { Directive, HostListener, ElementRef } from '@angular/core';
@Directive({selector: '[appDateRange]'})export class DateRangeDirective {constructor(private el: ElementRef) {}
@HostListener('input', ['$event']) onInput(event: Event): void {const inputElement = this.el.nativeElement as HTMLInputElement;const selectedDate = inputElement.value;const currentDate = new Date().toISOString().split('T')[0];
// Define las fechas de inicio y fin del rango
const startDate = '2023-10-15';
const endDate = '2023-10-23';
if (selectedDate < currentDate || (selectedDate >= startDate && selectedDate <= endDate)) {
inputElement.value = '';
}
}}
但这行不通。
答:
0赞
Timo Go
10/11/2023
#1
您可以使用“min”和“max”值阻止特定日期:
<input name="yourdate" type="date" min="2023-03-03" max="2023-06-06">
并特定于角度:
<input name="yourdate" type="date" min="{{startDate}}" max="{{endDate}}">
评论
0赞
Carlos López Monreal
10/11/2023
是的,但是过去的日子呢?我也想禁用它
0赞
Timo Go
10/11/2023
最小值设置可以开始的日期。因此,无法选择此日期之前的所有日期
0赞
Carlos López Monreal
10/11/2023
那么不能设置不同的日期范围吗?
评论