I am creating angular 6 dynamic forms by using link, https://angular.io/guide/dynamic-form
Here i have given following in question.service.ts,
getQuestions() {
let questions: DynamicBase<any>[] = [
new TextboxQuestion({
key: 'firstName',
label: 'First name',
type: 'text',
value: '',
required: true,
order: 1
}),
new TextboxQuestion({
key: 'emailAddress',
label: 'Email',
type: 'email',
order: 2
}),
new DropdownQuestion({
key: 'brave',
label: 'Bravery Rating',
options: [
{key: 'solid', value: 'Solid'},
{key: 'great', value: 'Great'},
{key: 'good', value: 'Good'},
{key: 'unproven', value: 'Unproven'}
],
order: 4
}),
];
return questions.sort((a, b) => a.order - b.order);
}
Here instead of
new TextboxQuestion({
key: 'firstName',
label: 'First name',
type: 'text',
value: '',
required: true,
order: 1
}),
I would like to load the data from the JSON,
"dynamicJSON": [
{
"key": "role_name",
"label": "Role Name",
"type": "text",
"value": "",
"required": true,
"order": 1
},
{
"key": "last_ame",
"label": "Last Name",
"type": "text",
"value": "",
"required": true,
"order": 2
}
]
For which i have used the following,
let newArray = dynamicJSON;
let questions: DynamicBase<any>[] = [
newArray.forEach(element => {
new TextboxQuestion(element)
});
];
return questions.sort((a, b) => a.order - b.order);
Where element gives the value in console as,
{key: "role_name", label: "Role Name", type: "text", value: "", required: true, …}
But whereas i am getting the error as key of undefined.. When i console questions also displays as [undefined]..
How to pass the dynamic json values inside the form object like textbox? Kindly help me to get out of it, stucked for a long..
You are not pushing TextboxQustion object in questions array. I have created sample example on stackblitz, please check.
Here is the ts code.
let newArray = this.dynamicJSON;
let questions: any = [ ]
newArray.forEach(element => {
questions.push(element)
});
questions.sort((a, b) => a.order - b.order);