Thomas Geulen
Thomas Geulen

Reputation: 610

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?

Upvotes: 5

Views: 5361

Answers (1)

Frank Modica
Frank Modica

Reputation: 10526

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;

Upvotes: 10

Related Questions