You should use the formArrayName directive and *ngFor as follows:
<form [formGroup]="form" (ngSubmit)="sayHello()"> <input formControlName="name"><br> <input formControlName="email"><br> <div formArrayName="username"> <div *ngFor="let user of username.controls; let i=index"> <my-child formControlName="i"></my-child> </div> </div> <button type="submit">Register</button> </form>
And with FormBuilder you should also use FormArray .
form = new FormGroup({ name: new FormControl('My Name'), username: new FormArray([ new FormControl("value"),// ControlValueAccesor is applied only to one control, not two. So you cannot use javascript object like you are using below this line. {firstName:"Anna", lastName:"Smith"}, {firstName:"Peter", lastName:"Jones"} ]) });
See this document for more information.
Case 2: FormGroup submission:
form = new FormGroup({ name: new FormControl('My Name'), username: new FormArray([ new FormGroup({ firstName: new FormControl('Anna'), lastName: new FormControl('Smith') }), new FormGroup({ firstName: new FormControl('Peper'), lastName: new FormControl('Jones') }), ]) })
If you are trying to pass FormGroup as ngModel parameters, you cannot!
Serginho
source share