我有一个formBuilder数组,需要更改他的值。
我尝试了setvalue,但只是改变了外观。
stackblitz
updateAddress(i,address) {
// (<FormControl>this.form.controls['adresses'][i].controls.estado).setValue('RR');
address.value.states = 'GOIAS'
}
initializeFormEmpty() {
this.form = this.formBuilder.group({
adresses: this.formBuilder.array([this.addressItem()])
})
}
addressItem(): FormGroup {
return this.formBuilder.group({
zip_code: this.formBuilder.control('', [Validators.required]),
states: this.formBuilder.control('RORAIMA', [Validators.required]),
})
}
答案 0 :(得分:2)
将您的updateAddress
函数更改为以下内容:
updateAddress(i,address) {
let myFormArray = this.form.get('adresses') as FormArray;
myFormArray.controls[i].get('states').setValue('GOIAS');
}
这是我认为您要完成的工作的working example。