How do i use formGroupName inside child components?
for example:
i have ParentFormComponent
parentForm: FormGroup;
constructor(private fb: FormBuilder, private http: Http) { }
ngOnInit() {
this.parentForm = this.fb.group({
_general: this.fb.group ({
ProjectName:''
})
})
}
In the html:
<form [formGroup]="parentForm" (ngSubmit)="submitForm()">
<div formGroupName="_general">
<mat-form-field>
<input matInput placeholder="Project name"
formControlName="ProjectName">
</mat-form-field>
</div>
</form>
it's working great but when i want to use child component it's not working:
<form [formGroup]="parentForm" (ngSubmit)="submitForm()">
<app-child [parentForm]='parentForm'></app-child>
</form>
when i insert it to the child component:
<div formGroupName="_general">
<mat-form-field>
<input matInput placeholder="Project name"
formControlName="ProjectName">
</mat-form-field>
</div>
and in the ts file
@Input() parentForm:FormGroup;
i"m getting error:
formGroupName 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).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…