繁体   English   中英

Angular Material 2 DatePicker,只有月份和年份

[英]Angular Material 2 DatePicker with only month and year

我一直在寻找一种方法来使角材料 datepicker 2 更改为仅年和月模式,但文档中没有该选项

我可以设置StartView,但选择月或年,更改为日间模式

<md-form-field>
     <input mdInput [mdDatepicker]="picker" placeholder="Choose a date">
     <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
     <md-datepicker #picker startView="year" [startAt]="startDate"></md-datepicker>
</md-form-field>

我想要这个: https : //plnkr.co/edit/eV2Kmt?p=preview

但是那个plunker是角材料1

有什么帮助吗? :)

这一次这是一个开放的错误。 看这个链接

看起来很快就会有一年的支持,一旦这个 PR被合并! 🎉

(但是,似乎它可能会出现在 5.1 版中,因为它被放置在里程碑5.1

在 HTML 中

<mat-datepicker #searchDate startView="year" (monthSelected)="monthSelectedHandler($event)"></mat-datepicker>

在您的控制器中

searchDateValue: FormControl;

ngOnInit() {
    this.searchDateValue = new FormControl(this.searchDate);
}

monthSelectedHandler(event) {
    const date = new Date(`${event._i.month + 1}/${event._i.date}/${event._i.year}`);
    this.searchDateValue.setValue(date);
    this.searchDatePicker.close();
}

查看这个 repo,它还有一个独立的月份选择器: https : //github.com/vlio20/angular-datepicker

它将以月份和年份格式提供有用的材料日期选择器。 检查 MM-YYYY 的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM