At first it helped me a lot, but then I found out that we complicate things too much. We do not need to create our own formControl, we can just pass formGroup to our child component. In the parent component instead
this.form = fb.group({ name:['Angular2 (Release Candidate!)'], username: [{firstName: 'First', lastName: 'Last'}], email:['My Email'] });
we initialize the username as a FormGroup instead of a control:
this.form = fb.group({ name:['Angular2 (Release Candidate!)'], username: fb.group({ firstName: ['First'], lastName: ['Last'] }), email:['My Email'] });
In the child component, we need an input property for FormGroup
@Input() usernameGroup: FormGroup;
In the child template:
<div [formGroup]="usernameGroup"> <input formControlName="firstName"> <input formControlName="lastName"> </div>
and then in the parent template:
<my-child [usernameGroup]="form.controls.username"></my-child>
For more information, check out this post: https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2
Creating your own formControl is really redundant, for more information on this, see here: http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
source share