如何 select only year on mat-datepicker
How to select only year on mat-datepicker
我正在开发一个 angular 8 的应用程序,其中我有一个仅包含年份数字的输入。
我用过mat-datepicker,只想选择年份。
<mat-form-field class="input-control">
<input matInput placeholder="{{'enter'|translate}}.." [formControl]="form.controls.openedAt"
[matDatepicker]="date" readonly>
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date startView="multi-year"></mat-datepicker>
</mat-form-field>
此代码先显示年视图,但当我选择年时,会打开月视图,然后是天视图。
那么我怎样才能 select 只有年份,或者在 mat-datepicker 上只显示年份视图?
此功能尚未完全实现。你可以在这里看到:
https://github.com/angular/components/issues/4853
现在你可以试试这个:
I did like this:
html:
<mat-form-field
appearance="outline">
<mat-label>Ano</mat-label>
<input [(ngModel)]="selectYear" [formControl]="dateForm" matInput
[matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker" ></mat-datepicker-
toggle>
<mat-datepicker
#picker
startView="multi-year"
(yearSelected)="chosenYearHandler($event, picker)"
></mat-datepicker>
</mat-form-field>
component:
export const MY_FORMATS = {
parse: {
dateInput: 'YYYY',
},
display: {
dateInput: 'YYYY',
monthYearLabel: 'YYYY',
monthYearA11yLabel: 'YYYY',
},
};
,
供应商:[
{
提供:日期适配器,
使用类:MomentDateAdapter,
部门:[MAT_DATE_LOCALE,MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
chosenYearHandler(ev, input){
let { _d } = ev;
this.selectYear = _d;
input._destroyPopup()
}
Its works well and very simple!!!
我正在开发一个 angular 8 的应用程序,其中我有一个仅包含年份数字的输入。 我用过mat-datepicker,只想选择年份。
<mat-form-field class="input-control">
<input matInput placeholder="{{'enter'|translate}}.." [formControl]="form.controls.openedAt"
[matDatepicker]="date" readonly>
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date startView="multi-year"></mat-datepicker>
</mat-form-field>
此代码先显示年视图,但当我选择年时,会打开月视图,然后是天视图。
那么我怎样才能 select 只有年份,或者在 mat-datepicker 上只显示年份视图?
此功能尚未完全实现。你可以在这里看到:
https://github.com/angular/components/issues/4853
现在你可以试试这个:
I did like this:
html:
<mat-form-field
appearance="outline">
<mat-label>Ano</mat-label>
<input [(ngModel)]="selectYear" [formControl]="dateForm" matInput
[matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker" ></mat-datepicker-
toggle>
<mat-datepicker
#picker
startView="multi-year"
(yearSelected)="chosenYearHandler($event, picker)"
></mat-datepicker>
</mat-form-field>
component:
export const MY_FORMATS = {
parse: {
dateInput: 'YYYY',
},
display: {
dateInput: 'YYYY',
monthYearLabel: 'YYYY',
monthYearA11yLabel: 'YYYY',
},
};
, 供应商:[ { 提供:日期适配器, 使用类:MomentDateAdapter, 部门:[MAT_DATE_LOCALE,MAT_MOMENT_DATE_ADAPTER_OPTIONS] },
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
chosenYearHandler(ev, input){
let { _d } = ev;
this.selectYear = _d;
input._destroyPopup()
}
Its works well and very simple!!!