【发布时间】:2019-04-06 10:13:34
【问题描述】:
我正在使用 Angular dynamic form 制作 Angular 6 应用程序
在这里我做了一个嵌套输入字段,在初始阶段将有两个输入文本框,点击添加按钮后,接下来的两个输入框将在每次点击添加按钮时追加。
这里一切正常。
这里我使用了 question-service.ts 中的值,
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "project_name",
label: "Project Name",
type: "text",
value: '',
required: true,
order: 1
}),
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "project_desc",
label: "Project Description",
type: "text",
value: '',
required: true,
order: 2
}),
new ArrayQuestion({
key: 'myArray',
value: '',
order: 3,
children: [
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "property_one",
label: "Property One",
type: "text",
value: '',
required: true,
order: 3
}),
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "property_two",
label: "Property Two",
type: "text",
value: '' ,
required: true,
order: 4
})
]
})
我需要更改的数据应该来自每个赞的 json,
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": true,
"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": "array",
"key": "myArray",
"value": "",
"order": "3",
"children": [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "property_one",
"label": "Property One",
"type": "text",
"value": "",
"required": true,
"order": 3
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "property_two",
"label": "Property Two",
"type": "text",
"value": "",
"required": true,
"order": 4
}
]
}
];
Stackblitz 没有 JSON:
https://stackblitz.com/edit/angular-x4a5b6-xcychx
Stackblitz 使用 JSON:
https://stackblitz.com/edit/angular-x4a5b6-u6ecpk
加载 JSON 时需要发生在没有 json 的 stacblitz 链接中发生的相同场景..
我在getQuestions()里面给出了如下的赞,
getQuestions() {
console.log(this.jsonData);
let questions: any = [];
this.jsonData.forEach(element => {
if (element.elementType === 'textbox') {
questions.push(new TextboxQuestion(element));
} else if (element.elementType === 'array') {
questions.push(new ArrayQuestion(element));
}
});
return questions.sort((a, b) => a.order - b.order);
}
}
对于普通文本框,它可以正常工作,但对于子文本框,它无法单击添加按钮(文本框未显示),子文本框不会被添加。
请帮助我实现link 1 中发生的相同结果,同时在link 2 中使用 JSON 时也需要发生 .. 并且请不要在所有内容中包含任何第三方库,所有内容都以核心角度进行。
【问题讨论】:
标签: javascript json angular typescript angular-reactive-forms