Angular Add Control to form from child component

Viewed 3792

In Angular 2, I have a child component that includes an input. I want to add this input (control) to the parent components form (NgForm).

I am currently using Template Driven Forms for simplicity.

I saw this answer, but think it is likely outdated?: Angular 2 Add control to parent component's form

Child Component Template: formInputName is an input binding so that I can reuse this component, and add the 'name' property dynamically.

<input class="text-input" [name]="formInputName" [id]="formInputName" type="text" [(ngModel)]="searchValue"
            (change)="onChange(searchValue)"
            (blur)="onBlur()"
            (focus)="onFocus()"
            [required]="isRequired">

On the parent component I have an instance of the form:

    @ViewChild('testForm') testForm: NgForm;

How can I add the child components control to this instance of NgForm? I'm not sure how to do it with addControl. Not sure what I need to add in the template, or how to do it programmatically in the controller.

Plunker: https://plnkr.co/edit/rDluyJduyHF7vclK9wRE?p=preview

1 Answers
Related