我希望我的 Angular Material 日期选择器只显示月/年。没有天。
这是我的日期选择器:
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose a date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
我看到以前的线程说明将模式设置为:
md-mode="month"
以上不再起作用。现在有什么解决办法?
最佳答案
这是一个对我有用的代码。我看到你正在尝试类似的东西。希望能帮助到你。.html
文件:
<mat-form-field>
<input matInput [matDatepicker]="dp2" [min]="sixMonthsAgo" [max]="today" placeholder="Select a Date" (click)="openDatePicker(dp2)">
<mat-datepicker-toggle matSuffix [for]="dp2"></mat-datepicker-toggle>
<mat-datepicker #dp2 startView="multi-year" (monthSelected)="closeDatePicker($event, dp2)"></mat-datepicker>
</mat-form-field>
来自
.ts
的片段文件: this.today = new Date();
this.sixMonthsAgo = new Date();
this.sixMonthsAgo.setMonth(this.today.getMonth() - 6);
openDatePicker(dp) {
dp.open();
}
closeDatePicker(eventData: any, dp?:any) {
// get month and year from eventData and close datepicker, thus not allowing user to select date
dp.close();
}
关于Angular Material Datepicker - 仅限月份和年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676974/