How to get value as type Number on this.formgroup.value()?

Sunil Behera picture Sunil Behera · Dec 3, 2018 · Viewed 12k times · Source

The value I get on submitting a form group is

{
  "name": "Sunil",
  "age": "23"
}

What I want is

{
  "name": "Sunil",
  "age": 23
}

My form group is as follows in my .ts file

myForm : FormGroup;
this.myForm = this._formbuilder.group({
  name: [''],
  age: [null]
});

Answer

user4676340 picture user4676340 · Dec 3, 2018

Convert it before sending it.

const value = { ...this.myForm.value, age: +this.myForm.value.age };

Or you can use an input of type number.

<input type="number" formControlName="age">