简体   繁体   English

角度响应形式,使用数组值更新控件

[英]Angular responsive form, updating control with array value

I'm trying to update the value of an array in a form control. 我正在尝试更新窗体控件中数组的值。 Simply clearing it out and attempt to set a new value results in an error. 简单清除它并尝试设置新值会导致错误。 Based on other SO answers I'm attempting change values directly via a loop. 基于其他SO答案,我正尝试直接通过循环更改值。 I don't get errors but the values don't update: 我没有收到错误,但是值没有更新:

  const voltage = this.dynamicValues.controls.voltage;

  this.voltageItems.sort();

  this.voltageItems.forEach((volt, i) => {
    const control = new FormControl(volt);
    if (voltage.value[i]) {
      (voltage as FormArray)[i] = control;
    } else {
      (voltage as FormArray).push(control);
    }
  });

  this.addComponentForm.updateValueAndValidity();

this.dynamicValues is a formGroup of controls, one of which is voltage. this.dynamicValues是一formGroup控件形式,其中一个是电压。 this.voltageItems is just an array of the selected voltages. this.voltageItems只是所选电压的数组。 Finally, this.addComponentForm is the parent formGroup that holds the other groups and controls. 最后, this.addComponentForm是保存其他组和控件的父formGroup I've tried placing the updateValueAndValidity against this.dynamicValues but that didn't resolve the issue. 我试着放置updateValueAndValiditythis.dynamicValues ,但没有解决问题。

The initial value set by the (voltage as FormArray).push(control); 初始值由(voltage as FormArray).push(control); sets fine, no issues, but the value isn't updating with the (voltage as FormArray)[i] = control; 设置正常,没有问题,但该值不会随着(voltage as FormArray)[i] = control;

If I log (voltage as FormArray)[i] after setting the new value it shows with the new value instead of what was originally set by the (voltage as FormArray).push(control); 如果我在设置新值后记录(voltage as FormArray)[i] ,它将显示新值,而不是最初由(voltage as FormArray).push(control);设置的值(voltage as FormArray).push(control); but it's not reflecting that in the form. 但这并没有在表格中反映出来。

logging out voltage or this.dynamicValues.controls.voltage both show the original values as though the foreach never ran. 注销voltagethis.dynamicValues.controls.voltage都显示原始值,就像foreach从未运行过一样。

SO as i understood you have addComponentForm is parent form group and inside addComponentForm you have dynamicValues is another form group and inside this i assume you have voltage as form array(as you are trying to push inside). 因此,据我dynamicValues您有addComponentForm是父表单组,而在addComponentForm您具有dynamicValues是另一个表单组,在此内部我假设您有voltage作为表单数组(当您尝试推入内部时)。 Please look at following code which worked for me. 请查看以下对我有用的代码。

addComponentForm: FormGroup;
  voltageItems = ['a', 'b', 'c'];

constructor(private formBuilder: FormBuilder) {}

  public ngOnInit() {
    this.addComponentForm = this.formBuilder.group({
     dynamicValues: this.formBuilder.group({
                         voltage: this.formBuilder.array([]) 
                    }),
      otherControl: [''] 
    });

    const voltage = this.addComponentForm.get('dynamicValues').get('voltage');

   this.voltageItems.sort();

  this.voltageItems.forEach((volt, i) => {
    const control = new FormControl(volt);
    if (voltage.value[i]) {
      (voltage as FormArray)[i] = control;
    } else {
      (voltage as FormArray).push(control);
    }
  });

  console.log(this.addComponentForm);

  this.addComponentForm.updateValueAndValidity();
  }

在此处输入图片说明

Really it's most simple that all this. 真的,这一切最简单。 If this.voltage is a FormArray and this.voltageItems an array of values (else, please updated the question with this data) 如果this.voltage是一个FormArray,而this.voltageItems是一个值数组(否则,请使用此数据更新问题)

this.voltage=new FormArray(this.voltageItems.map(x=>new FormControl(x))

//or 
this.voltage.clear();
this.voltageItems.forEach(x=>{
    this.voltage.push(new FormControl(x));
})

A Form array is not only just an array of FormControls (or an array of FormGroups) 一个Form数组不仅是一个FormControls数组(或一个FormGroups数组)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM