[英]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: JSON文件如下所示:
{
"ITEMS": [
{
"NAME": "aaa",
"QUANTITY": ["140", "60"]
}
]
}
Thanks for your help :) 谢谢你的帮助 :)
Here have an example, item.model.ts
and his application at app.component.ts
. 这里有一个示例
item.model.ts
及其在app.component.ts
应用程序。
https://stackblitz.com/edit/angular-s3obqy?embed=1&file=src/app/app.component.html 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
: 对于这种字符串化对象,您可以在
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}];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.