Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Cast FormGroup value to interface

I am trying to cast the content of my FormGroup value into an interface which I want use for posting something to my Web Api.

My interface looks like this:

export interface MoneyItemI {
  Description: string;
  Amount: number;
}

My submit methods looks like this:

onSubmit() {
    let jsonString = JSON.stringify(this.itemForm.value);
    let mi = <MoneyItemI>JSON.parse(jsonString);
}

I can see that I get an object created with JSON.parse but unfortunately it does not look like it an valid MoneyItemI object for me.

Property 'Amount' for example is not a number. It is assigned like a string.

How can I create a valid interface with the value of my FormGroup?

like image 409
Thomas Geulen Avatar asked Apr 27 '17 10:04

Thomas Geulen


1 Answers

Does this.itemForm.value have the correct Amount and Description properties before you call JSON.stringify(this.itemForm.value)?

If so, you should be able to just do:

let mi = <MoneyItemI>this.itemForm.value;

like image 146
Frank Modica Avatar answered Oct 22 '22 04:10

Frank Modica