如何更新FormArrays中的值

时间:2019-07-06 22:50:38

标签: angular

我有一个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]),
    })
  }

stackblitz

1 个答案:

答案 0 :(得分:2)

将您的updateAddress函数更改为以下内容:

  updateAddress(i,address) {
    let myFormArray = this.form.get('adresses') as FormArray;
    myFormArray.controls[i].get('states').setValue('GOIAS');
  }

这是我认为您要完成的工作的working example