问题描述
我在使用Angular反应形式的嵌套FormArray时遇到了麻烦.我的一个表单数组正确返回为FormArray,另一个返回为FormControl.在initialMaterials()函数中,我有两个console.logs. console.log(control)
返回一个FormControl项,而 console.log(this.objectServiceJobsArray)
返回一个FormArray.
I'm having trouble with nested FormArrays in Angular reactive forms. One of my form arrays correctly returns as a FormArray and the other returns as FormControl. In the initialMaterials() function I have two console.logs. console.log(control)
returns a FormControl item and console.log(this.objectServiceJobsArray)
returns a FormArray.
我需要能够向阵列中的特定作业添加材料,并在必要时以表格的形式对其进行更改.
I need to be able to add materials to specific jobs in the array and change them in the form when necessary.
this.objectServiceForm = this.formBuilder.group({
onHolidays: [this.objectService.onHolidays],
objectServiceJobs: this.formBuilder.array([this.objectServiceJobs()]),
isBillable: [this.objectService.isBillable],
defaultPrice: [this.objectService.defaultPrice],
pricePerHour: [this.objectService.pricePerHour],
doneWeekly: [this.doneWeekly],
});
objectServiceJobs(): FormGroup {
return this.formBuilder.group({
job: [''],
workDetail: [''],
competentWorkers: [[]],
materials: this.formBuilder.array([this.objectServiceJobMaterials()])
});
}
objectServiceJobMaterials(): FormGroup {
return this.formBuilder.group({
material: [null],
quantity: [null]
});
}
initialMaterials(job) {
const index = (<FormArray>this.objectServiceForm.get('objectServiceJobs')).controls.findIndex(x => x.value.job.id === job.id);
const control = (<FormArray>this.objectServiceForm.controls['objectServiceJobs']).at(index).get('materials') as FormArray;
console.log(control);
console.log(this.objectServiceJobsArray);
// job.materials.forEach(mat => {
// this.objectServiceJobsArray[index].materials.push(this.makeMaterialFormGroup(mat));
// });
}
推荐答案
我在IDE中尝试了您的代码,但是更改了提取控件的样式,并且可以看到console.log(control)使我返回为FormArray.
I tried your code in my IDE but changed the style of extracting controls and I can see that console.log(control) returns me as FormArray.
initialMaterials(job) {
const objectServiceJobs = this.objectServiceForm.get('objectServiceJobs') as FormArray;
const index = objectServiceJobs.controls.findIndex(x => x.value.job.id === job.id);
const control = objectServiceJobs.at(index).get('materials') as FormArray;
console.log(control);
}
这篇关于FormArray返回为FormControl而不是FormArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!