代码之家  ›  专栏  ›  技术社区  ›  JadedEric

角度窗体组值更改将属性转换为数组

  •  0
  • JadedEric  · 技术社区  · 7 年前

    我正在观察fromGroup上的更改,但是,似乎对于formGroup中的空/空属性,它会将这些属性转换为空数组。

    我的formGroup实例化如下:

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

    没什么特别的,只是在中添加了默认值。

    enter image description here

    但是,当valueChanges(…)启动时,值将转换为空数组?

    enter image description here

    我在这里做错了什么吗,因为这种行为并不像预期的那样。我是不是应该通过一个表单生成器?

    更新

      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 回复  |  直到 7 年前
        1
  •  0
  •   JadedEric    7 年前

    它将在团队提交的代码中显示一个开发人员,该代码迭代原始数组的键,并用数组的值替换该值,并将这段代码添加为FormGroup对象的重写。

    推荐文章