[英]PatchValue on a nested FormArray
I have this form that has a nested form array that holds date values.我有一个包含日期值的嵌套表单数组的表单。 When the user selects a date, a function is called that formats the date and should update the form's value.
当用户选择一个日期时,会调用一个函数来格式化日期并更新表单的值。 But, I am having a hard time understanding how to get access to the specific formcontrol in the array... This is the relevant code
但是,我很难理解如何访问数组中的特定表单控件......这是相关代码
constructor(private formBuilder: FormBuilder, private datepipe: DatePipe) {
this.form = this.formBuilder.group({
loading: this.formBuilder.array([])
});
}
get loadingForm(){
return this.form.get('loading') as FormArray
}
addLoadingTimes(){
const loading = this.formBuilder.group({
StartLoadDate: [],
EndLoadDate: []
})
this.loadingForm.push(loading);
}
//format date from datepicker
changeDateTimeEvent(event:MatDatepickerInputEvent<Date>, index, controlName) {
const d = new Date(event.value);
const date = this.datepipe.transform(d, 'yyyyMMddHHMMss');
this.form.controls['loading'][controlName].at(index).patchValue(date);
}
Right now, I am trying to update the value with this line:现在,我正在尝试使用以下行更新值:
this.form.controls['loading'][controlName].at(index).patchValue(date);
but, apparently, is wrong... Could anybody help?但是,显然是错误的......有人可以帮忙吗?
Here is a blitz https://stackblitz.com/edit/formarray-patchvalue这是一个闪电战https://stackblitz.com/edit/formarray-patchvalue
FormArray method has at method to access specific control use that to setValue. FormArray 方法有at方法来访问特定的控件,使用它来设置值。
Try this:尝试这个:
changeDateTimeEvent(event:MatDatepickerInputEvent<Date>, index, controlName) {
const d = new Date(event.value);
const day = this.datepipe.transform(d, 'yyyyMMddHHMMss');
let formName : string;
this.loadingForm.at(index).get(controlName).setValue(day);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.