我正在尝试构建一个数据驱动的表单,输入来自另一个组件,如下所示:
<form [formGroup]="signupForm" (ngSubmit)="onSubmit()"> <app-form-group [name]="name"></app-form-group> <app-form-group [name]="email"></app-form-group> <app-form-group [name]="other"></app-form-group> </form>
app-form-group组件看起来像这样:
<div class="form-group"> <label class="col-md-2 control-label">{{Name}}</label> <div class="col-md-9"> <input class="form-control" [name]="name" [formControlName]="formCtrlName"> </div>
问题是formControlName需要一个formGroupdirective,因此我收到此错误:
Error : Error in ./FormGroupComponent class FormGroupComponent - inline template:3:58 caused by: formControlName must be used with a parent formGroup directive.You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class).
有没有办法解决这个问题?
您应该在app-form-group Component中使用FormGroup [formGroup] =“signupForm”.您可以使用此代码:
<div class="form-group" [formGroup]="signupForm"> <label class="col-md-2 control-label">{{Name}}</label> <div class="col-md-9"> <input class="form-control" [name]="name" [formControlName]="formCtrlName"> </div>