我想禁用两个日期之间选定的角度材料。例如,用户不能选择2021-03-02
和2011-03-02
之间的任何日期。
我写下面的代码:
<input matInput [max]="maxDate" [min]="minDate" [matDatepicker]="picker6"/>
maxDate = new Date();
minDate =new Date().setFullYear(this.maxDate.getFullYear() - 10)
但是没有成功
演示怎么了?我怎样才能解决这个问题?
请将您的html文件替换为
your.component.html
<mat-form-field>
<mat-label>Choose Start date</mat-label>
<input matInput [max]="unavailabilityForm.controls.startDate.value" [matDatepicker]="picker1" />
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker [dateClass]="dateClass" #picker1></mat-datepicker>
</mat-form-field>
<mat-form-field class="example-full-width" appearance="fill">
<mat-label>Choose End date</mat-label>
<input matInput [min]="unavailabilityForm.controls.endDate.value" [matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker [dateClass]="dateClass" #picker></mat-datepicker>
</mat-form-field>
your.component.ts
import { Component, OnInit, ViewEncapsulation } from "@angular/core";
import { FormBuilder } from "@angular/forms";
/** @title Datepicker with custom date classes */
@Component({
selector: "datepicker-date-class-example",
templateUrl: "datepicker-date-class-example.html",
styleUrls: ["datepicker-date-class-example.css"],
encapsulation: ViewEncapsulation.None
})
export class DatepickerDateClassExample implements OnInit {
unavailabilityForm: any;
maxDate = new Date();
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
let startDateTimeStamp = new Date().setFullYear(new Date().getFullYear() - 10);
this.unavailabilityForm = this.formBuilder.group({
startDate: [new Date(startDateTimeStamp)],
endDate: [new Date()]
});
}
}
这会解决你的问题。