typescript 只有月份和年份的 Angular Material 2 DatePicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47410737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:03:13  来源:igfitidea点击:

Angular Material 2 DatePicker with only month and year

angulartypescriptdatepickerangular-material2

提问by José Rodríguez

I was looking for a way to make the angular material datepicker 2 change to only year and month mode but it does not have that option in the documentation

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

I can set the startView but when month or year is selected, change to Day mode

我可以设置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>

I want this: https://plnkr.co/edit/eV2Kmt?p=preview

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

But that plunker is in angular material 1

但是那个plunker是角材料1

Any help? :)

有什么帮助吗?:)

采纳答案by Ankit Kapoor

For this time this is an open bug. See this link

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

回答by Ga?tan Mallet

In HTML

在 HTML 中

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

In your controller

在您的控制器中

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();
}

回答by vlio20

Checkout this repo, it has also a stand alone month picker: https://github.com/vlio20/angular-datepicker

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

回答by Nagnath Mungade

it will helpful material datepicker with month and year format.check link for MM-YYYY

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

回答by Edric

Looks like there's soon gonna be year support soon, once this PRis merged!

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

(However, it appears that it probably will appear in release 5.1 since it was placed in milestone 5.1)

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