Angular 角反应式装订不起作用
我尝试在此处创建嵌套的反应式表单: 这是项目继承人:Angular 角反应式装订不起作用,angular,angular2-forms,angular-reactive-forms,Angular,Angular2 Forms,Angular Reactive Forms,我尝试在此处创建嵌套的反应式表单: 这是项目继承人: ---create company form (hello.component.ts) --- company details form (company-details.component.ts) --- [ employee details form (employee-details.component.ts) employee details form employee
---create company form (hello.component.ts)
--- company details form (company-details.component.ts)
--- [
employee details form (employee-details.component.ts)
employee details form
employee details form ...
]
对于此类嵌套表单,我必须使用ViewProviders
和FormGroupDirective
,如中所示:
第一个嵌套表单(company details.component.ts)按预期工作
但是在FormArray
中添加了*ngFor
的第二个表单未正确绑定到FormGroupDirective
当您键入公司详细信息并按打印Json时,打印的Json将是正确的。但是,当您添加一两名员工时,json中不会打印员工详细信息
我知道还有其他手动方式来完成同样的事情,但我正在寻找一个干净的解决方案,它不需要任何额外的功能
任何帮助都将不胜感激
提前感谢您需要对代码进行一些更改 1.-employed.details.component.html
<!---see that is [formGroup], not formGroupName-->
<div [formGroup]="employee">
<h4 class="row">Employee no. {{index+1}}</h4>
<div class='col-md-8'>
<input formControlName="name" placeholder="Name">
</div>
<div class='col-md-8'>
<input formControlName="planet" placeholder="Planet">
</div>
</div>
最后,在删除employe时,必须删除数组中的元素
removeEmployee() {
this.employeesCount -= 1;
(this.form.get('employees') as FormArray).removeAt(this.employeesCount);
}
查看您是否注意到,
此.fgd
在您的员工详细信息中未定义?您能再描述一下“清洁解决方案”吗?节省了我的时间!也更深入地了解了反应形式!非常感谢你!
removeEmployee() {
this.employeesCount -= 1;
(this.form.get('employees') as FormArray).removeAt(this.employeesCount);
}