简体   繁体   中英

How do I declare a model class using Typescript for Angular reactive Form

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

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

There is the model:

export class DataModel {
    public ITEMS: any[];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM