简体   繁体   English

如何使用变量在 Angular Material datepicker 上设置日期值?

[英]How do you set the date value on Angular Material datepicker with a variable?

I need to be able to edit the date of an object in an array.我需要能够编辑数组中 object 的日期。 When I try to pass the selected object's date into the Material datepicker, it doesn't work.当我尝试将所选对象的日期传递到 Material 日期选择器时,它不起作用。 However, it works if you type in a date as a string.但是,如果您将日期输入为字符串,它会起作用。 Please see my Stackblitz example . 请参阅我的 Stackblitz 示例

You don't want to set an initial value of the form control.您不想设置表单控件的初始值。 Instead, you want to set the value of the form control in the moment you hit the "edit" button.相反,您想在点击“编辑”按钮的那一刻设置表单控件的值。

Updated code:更新代码:

datePickerControl = new FormControl();

editDate(o: any) {
  this.selectedDate = o;
  this.datePickerControl.setValue(o.date1);
}

See also the updated Stackblitz另请参阅更新的 Stackblitz

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

相关问题 无法在 Angular 材料日期选择器中设置日期 - Unable to set date in Angular material datepicker 在 Angular Material 日期选择器中本地化日期值 - Localising the date value in Angular Material datepicker 角材料(md-datepicker)-如何在一页中将不同的日期格式设置为不同的日期选择器 - angular material (md-datepicker) - how to set different date format to different datepicker in one page 如何将Angular Material md-datepicker的日期值初始化为任何指定日期 - How to initialize the date value of Angular Material md-datepicker to any specified date 使用 ngmodel - Angular 将值设置为角度材​​料日期选择器 - Set value to angular material datepicker using ngmodel - Angular 如何设置与 KnockoutJS 绑定的 jQuery DatePicker 的默认值? - How do you set the default value of a jQuery DatePicker that is bound with KnockoutJS? Angular 2 Material 2日期选择器日期格式 - Angular 2 Material 2 datepicker date format 如何在 Angular 6 中将材料日期选择器日期值格式化为“MM-DD-YYY”格式? - How to format material datepicker date value to "MM-DD-YYY" format in Angular 6? 设置值角度材料日期选择器,从外部输入 - Set value angular material datepicker, from input outside 如何动态设置Angular Material md-datepicker指令的md-min-date / md-max-date? - How to dynamically set md-min-date/md-max-date of Angular Material md-datepicker directive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM