我想像这样在数组中设置值:
this.form.controls[name].setValue('name')
但我正在使用数组形式,这不起作用,即使我显式传递数组索引
例如,这是我的表单数组,我想做的是在函数中设置值
user: FormGroup;
users: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.user = this.buildGroup();
this.users = this.fb.group({
data: this.fb.array([this.user])
});
}
get fData() {
return this.users.get('data') as FormArray;
}
buildGroup() {
return this.fb.group({
name: ['', [Validators.required, Validators.minLength(2)]],
account: this.fb.group({
email: ['', Validators.required],
confirm: ['', Validators.required]
})
});
}
setValue(index) {
// This doesn't work
this.fData[index].controls[name].setValue('name')
}
onSubmit() {
this.fData.push(this.buildGroup());
const {valid, value} = this.fData;
console.log(valid, value);
}