4

I make a form using DynamicForm it work perfect but when try to get the values using form.value or form.getRawValue() all files are return like string, how I make to the number files as return as integer in the JSON file

Example

actually I get a JSON like this

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

but I need this:

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

EDIT

The formControl constructor

const form = new FormGroup({
  name : new FormControl(undefined  || '', Validators.required),
  age  : new FormControl(undefined  || '')
}); 
oriaj
  • 768
  • 1
  • 16
  • 33

2 Answers2

3

try putting null into first param of FormControl

kit
  • 4,890
  • 3
  • 24
  • 23
0

If anyone is trying to convert input type="number" to an integer with an array of controls using angular dynamic forms.

This snipped returns the actual control that was changed.

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}))
})

This will return a value of type integer.

Levarne Sobotker
  • 1,140
  • 12
  • 12