0

I would like to create a model for reactive Form.

The JSON File look like this:

      {
        "ITEMS": [
          {
            "NAME": "aaa",
            "QUANTITY": ["140", "60"]
          }
        ]
      }

Thanks for your help :)

2
  • I think this question could help you Commented Jun 24, 2019 at 21:49
  • Thanks, but my question is not about making the Form. It's about making the model i will use in a reactive Form :) Commented Jun 25, 2019 at 9:50

2 Answers 2

1

Here have an example, item.model.ts and his application at app.component.ts.

https://stackblitz.com/edit/angular-s3obqy?embed=1&file=src/app/app.component.html

In your case with this stringified object you can parse like that at app.component.ts :

 yourString = ` {
        "ITEMS": [
          {
            "NAME": "aaa",
            "QUANTITY": ["140", "60"]
          }
        ]
      }`;

  foo: Item = JSON.parse(this.yourString);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but that is not what i want and it's not reflecting the JSON file :(
it should be something like this :
export class DataModel { const ITEMS: [string, string[]]; const NAME: string; const QUANTITY: string[]; constructor() { this.ITEMS[{this.NAME, this.QUANTITY[]}]; } }
ITEMS is an array that containing a string call NAME and an array of string call QUANTITY :)
``` yourString = ` { "ITEMS": [ { "NAME": "aaa", "QUANTITY": ["140", "60"] } ] }`; foo: Item = JSON.parse(this.yourString); ```
|
0

There is the model:

export class DataModel {
    public ITEMS: any[];

    constructor(public NAME: string, public QUANTITY: Array<string>) {
        this.ITEMS = [{NAME, QUANTITY}];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.