[英]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.