在我的表单中添加了一个FormGroups数组之后,我无法在FormGroup中调用setValue
这就是我尝试过的:
this.formGroup.controls.formArray.get('input').setValue('test');
和
this.formGroup.get('formArray').get('input').setValue('test');
抛出:
Cannot read property 'setValue' of null
TS:
get formArray(): AbstractControl | null { return this.formGroup.get('formArray'); }
this.formGroup = this.fb.group({
formArray: this.fb.array([
this.fb.group({
input: [null, [Validators.required]]
}),
this.fb.group({
anotherInput: [null, [Validators.required]]
}),
])
});我做错了什么?
发布于 2019-07-26 17:55:43
表单数组的控件是AbstractControls的数组,在tihs的情况下,该数组中有组,因此需要作为一个数组访问它:
this.formGroup.get('formArray').get('0').get('input').setValue('test');在与您想要的组对应的表单数组的索引处运行get,然后可以在该数组中访问组上的控件。
您可能还想更改您的getter:
get formArray(): FormArray { return this.formGroup.get('formArray') as FormArray; }https://stackoverflow.com/questions/57224927
复制相似问题