【发布时间】:2019-04-24 08:26:37
【问题描述】:
我正在使用角度动态形式制作角度应用程序。这里我正在制作我有一个选择框的表单。
点击选择框我想生成需要数组的输入框..
这里连文本框的生成都不起作用..
HTML
<div *ngFor="let item of array">
{{item.value}} is the parent
<div *ngFor="let child of item.templateChild">
{{child.property_name}}
<input type="text" [value]="child.property_name">
<div *ngFor="let partThree of questionsParthree">
<ng-container>
<app-question [question]="partThree" [form]="formPartThree"></app-question>
</ng-container>
</div>
</div>
</div>
TS:
this.questionsPartThree = this.service.getQuestions(element);
console.log(values);
this.formPartThree = this.qcs.toFormGroup(this.questionsPartThree);
this.formJoin = new FormGroup({ form1: this.form, form2: this.formPartTwo, template_data: this.formPartThree });
尝试在上面列出的 html 中创建 formArray,但我收到错误为 cannot find control name template_properties。
为了混淆和提出不清楚的问题,我已经减少了代码和你将在演示中获得的所有内容..
工作中的堆栈闪电战: https://stackblitz.com/edit/angular-x4a5b6-wyqdzn
表单的当前输出是,
{
"form1": {
"project_name": "",
"project_desc": ""
},
"form2": {
"property_one": "",
"property_two": ""
},
"template_data": {
"template_details": [
{
"Propertyone": "",
"Propertytwo": "",
"Propertythree": "",
"Propertyfour": "",
"Propertyfive": ""
}
]
}
}
预期输出:
{
"form1": {
"project_name": "",
"project_desc": ""
},
"form2": {
"property_one": "",
"property_two": ""
},
"template_details" : [
{ "template_name": "Template One",
"templateChild" : [{"property_one": "", "property_two":""}]
},
{ "template_name": "Template Two",
"templateChild" : [{"property_three": "", "property_four":"",
"property_five":""}]
}
]
}
如果我选择了多个选项(因为选择框是多选),以下内容预计会在template_properties 内形成。
表单分三部分,第一部分和第二部分不要管。选择框只和第三部分有关。
请参考上面的预期输出以获得 JSON 格式的结构.. 如果我选择两个,那么它不应该替换它应该在数组 template_properties.. 中获取多个对象的值p>
{ "template_name": "Template One",
"templateChild" : [{"property_one": "", "property_two":""}]
},
{ "template_name": "Template Two",
"templateChild" : [{"property_three": "", "property_four":"",
"property_five":""}]
}
苦苦挣扎,但我无法找到正确的解决方案。任何有角的人请帮助我使用demo 实现当前输出的预期输出..
【问题讨论】:
标签: angular typescript angular2-forms angular-reactive-forms angular-forms