Currently, I am developing an Angular 6 application that involves creating a dynamic form using data obtained from a JSON file.
JSON Data:
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
}
];
In the dropdown section above, I aim to retrieve the options
array from a service call by making it dynamic instead of hardcoding as it is currently shown.
The service function in dynamic-form.component.ts
:
getDropdownValues(url, token) {
this.service.get(url, token).subscribe(res => {
console.log(res.data);
});
}
The response from res.data consists of:
{
data: [
{ "key": 'average', "value": 'Average' },
{ "key": 'good', "value": 'Good' },
{ "key": 'great', "value": 'Great' }
]
}
This array will serve as the options
for the dropdown in the JSON data structure.
Though the JSON is currently included in the .ts
file, it will eventually be moved to a separate .json
file.
For a demonstration, check out the working stackblitz: https://stackblitz.com/edit/angular-x4a5b6-ng8m4z
I would appreciate guidance on integrating the data fetched from the service (res.data) into the dropdown options
within the JSON.