我正在观察fromGroup上的更改,但是,似乎对于formGroup中的空/空属性,它会将这些属性转换为空数组。
我的formGroup实例化如下:
static create(validators?: ValidatorFn[]): FormGroup {
return new FormGroup({
estimatedSpend: new FormControl(0),
currency: new FormControl(0),
department: new FormControl('')
}, validators);
}
没什么特别的,只是在中添加了默认值。
但是,当valueChanges(…)启动时,值将转换为空数组?
我在这里做错了什么吗,因为这种行为并不像预期的那样。我是不是应该通过一个表单生成器?
更新
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);
}
}