Angular FormGroup valueChanges将属性转换为数组

时间:2019-01-14 11:37:09

标签: angular angular-reactive-forms

我正在监视fromGroup上的更改,但是,对于formGroup中的空/空属性,似乎会将其转换为空数组。

我的formGroup实例如下:

  static create(validators?: ValidatorFn[]): FormGroup {
    return new FormGroup({
      estimatedSpend: new FormControl(0),
      currency: new FormControl(0),
      department: new FormControl('')
    }, validators);
  }

没什么,只是在其中添加了默认值。

当我在Chrome DevTools中查看时,formGroup的值是正确的:

enter image description here

但是,当valueChanges(...)插入时,值将转换为空数组吗?

enter image description here

我在这里做错什么了吗,因为这种行为不是预期的。也许我应该通过FormBuilder吗?

更新

  instantiateFormGroup(group: any): void {
    this.formGroup = new FormGroup(group);
  }

此方法是在基本组件中定义的,因为我有很多组件可以重复执行此步骤。

在扩展基础的每个组件中,我都会做类似的事情:

  constructor(cdr: ChangeDetectorRef) {
    super(cdr);

    this.instantiateFormGroup({
      clientQuestions: ClientQuestionsDTOFormGroup.create()
    });
  }

我在构造函数中执行此操作,因为我重写了默认路由器插座的activate方法来执行特定操作(完全无关)。

.create()方法:

export class ClientQuestionsDTOFormGroup {

  static create(validators?: ValidatorFn[]): FormGroup {
    return new FormGroup({
      estimatedSpend: new FormControl(0),
      currency: new FormControl(0),
      department: new FormControl('')
    }, validators);
  }
}

1 个答案:

答案 0 :(得分:0)

出现在团队中的开发人员承诺了代码,该代码迭代原始数组的键,并用数组的值替换该值,并将这段代码添加为FormGroup对象的替代。

相关问题