I have created an app with dynamic forms as explained in this tutorial: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html
Now I want to load the questions from a json file on a server instead of hard coded questions.
The JSON looks like this:
{
"ventureReason":[
{
"label": "Venture Reason",
"controlType": "dropdown",
"options": [
{
"key": "solid",
"value": "Solid"
},
{
"key": "great",
"value": "Great"
}
]
}
],
"blablabla": [
{
"label": "Blablabla",
"controlType": "textbox",
"options": [
]
}
]
}
In the QuestionService
I want to build the questions based on value of controlType
. But item.controlType
always returns undefined. data
contains the whole json file.
export class QuestionService {
questionUrl:string = `db`;
data:any;
questions : QuestionBase<any>[] = [];
constructor(
private http: HttpService
){}
getQuestions() {
return this.http.get(this.questionUrl)
.map(res => [res.json()])
.map(data => {
this.data = data;
return this.buildQuestions(data);
})
}
buildQuestions(data) {
console.log("data: "+ JSON.stringify(data)); //whole json file
let questions = [];
data.forEach((item: QuestionGroup) => {
console.log("item: " + JSON.stringify(item)); //whole json file
console.log("item: " + JSON.stringify(item.controlType)); //undefined
if (item.controlType == 'dropdown') {
item.controlType = 'dropdown';
questions.push(new DropdownQuestion(item));
}
else if (item.controlType == 'textbox') {
item.controlType = 'textbox';
questions.push(new TextboxQuestion(item));
}
});
return questions;
}
}
How could I acces the controlType
to build the questions?