4

我使用DynamicForm制作了一个表单,它工作得很好,但是当尝试使用form.valueform.getRawValue()所有文件都像字符串一样返回时,我如何将number文件作为integerJSON 文件中的返回

例子

实际上我得到了这样的 JSON

{
  "name": "home",
  "age": "12"
}

但我需要这个:

{
  "name": "home",
  "age": 12
}

编辑

formControl 构造函数

const form = new FormGroup({
  name : new FormControl(undefined  || '', Validators.required),
  age  : new FormControl(undefined  || '')
}); 
4

2 回答 2

2

尝试将 null 放入 FormControl 的第一个参数

于 2016-09-27T07:20:42.870 回答
0

如果有人试图将输入 type="number" 转换为使用角度动态形式的控件数组的整数。

此片段返回已更改的实际控件。

const formArray = this.parentFormGroup.controls['obligationList'] as FormArray
formArray.controls.forEach(control => {
    control.valueChanges
        .debounceTime(800)
        .distinctUntilChanged()
        .takeUntil(this.ngUnsubscribe)
        .subscribe(() => control.patchValue({amountPayable: parseInt(control.value['amountPayable'], 10)}, {emitEvent : false}))
})

这将返回一个整数类型的值。

于 2020-01-06T15:41:13.747 回答