在Angular中,当使用嵌套的FormArray时,如果找不到指定名称的控件,可以按照以下步骤解决问题:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {
myForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.myForm = this.fb.group({
nestedArray: this.fb.array([
this.fb.group({
'0-1': ''
})
])
});
}
get nestedArray(): FormArray {
return this.myForm.get('nestedArray') as FormArray;
}
}
const control = this.nestedArray.at(0).get('0-1');
通过按照以上步骤检查和调试你的代码,应该能够解决找不到指定名称控件的问题。
上一篇:Angular嵌套对象问题