3
votes

Let's say I have something like this:

<form>
   <my-component></my-component>
</form>

"my-component" in it's template has an "input" element.

How can I make my "form" to be aware of that field inside the component? I use template-driven form approach and I would like it to stay this way. The input has the "name" and "ngModel" set.

2
You'll have to bind the value into and out of the component. - jonrsharpe

2 Answers

3
votes

You may want to use FormGroup (https://angular.io/docs/ts/latest/api/forms/index/FormGroup-class.html).

  • Create a FormGroup object in your parent component.
  • Use 2 way binding to pass this FormGroup to your my-component.
  • Inside your my-component, add your input to the FormGroup.
0
votes

Angular Form, either template-driven or model-driven (reactive), doesn't look into the component's template. It will communicate with the child components only if they implement ControlValueAccessor interface.

More details can be found in this answer:

https://stackoverflow.com/a/41286138/324224