【发布时间】:2019-04-15 02:49:29
【问题描述】:
我正在制作 Angular 6 应用程序,我正在制作一个 Angular 动态表单,其中数据作为动态 来自 JSON。
JSON:
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": false,
"minlength": 3,
"maxlength": 20,
"order": 1
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"required": true,
"order": 2
},
{
"elementType": "dropdown",
"key": 'project',
"label": 'Project Rating',
"options": [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
],
"order": 3
}
];
在下拉列表中,我想从服务调用中获取 options 数组。
到目前为止,您可以看到我已经在选项中对其进行了硬编码..
服务于dynamic-form.component.ts:
getDropdownValues(url,token) {
this.service.get(url,token).subscribe(res => {
console.log(res.data);
});
}
res.data 返回以下内容,
{
data: [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
]
}
这个数据数组将是 JSON 格式的 options..
到目前为止,我已经在 .ts 文件中提供了 JSON,但稍后它将是一个单独的 .json 文件。
工作中的堆栈闪电战:https://stackblitz.com/edit/angular-x4a5b6-ng8m4z
请帮助我将来自服务 (res.data) 的数据放置到 JSON 中的下拉列表 options..
【问题讨论】:
-
目前,
jsonData是一个数组。options是否总是在第三个对象中,还是需要寻找它? -
@user184994, 没有订单可能随时更改.. 一切都会随时更改,但对于选项数组,将放置 res.data 值..
-
@Nik,是的,它绝对会与下拉菜单一起使用..
-
可以使用
jsonData.find(d => d.elementType === "dropdown").options从jsonData获取options数组 -
@user184994,我是 Angular 的新手。如果你用 stackblitz 将它作为答案来详细说明,这对我会有帮助。
标签: javascript json angular typescript angular-reactive-forms