Serge Intern
Serge Intern

Reputation: 2969

Add FormGroup at Component level in Angular 2

I have a component with a template like this:

<td>
  <input type="text" placeholder="Date: jour/mois/année" formControlName="dateDebut" >
</td>
<td>
  <input type="text" placeholder="Date: jour/mois/année" formControlName="dateFin">
</td>

As you might have guessed, the component will be applied (selector: '[app-xxx]') on tr elements.

I need (and want) [formGroup] to be applied at this component level. How can it be done?

I tried (I'm new at this) the following withouth success:

  @HostBinding('[formGroup]') formGroup: FormGroup;

Upvotes: 0

Views: 278

Answers (1)

Tiep Phan
Tiep Phan

Reputation: 12596

try this

child.ts

@Input() form: FormGroup;

template:

<td [formGroup]="form">
  <input type="text" placeholder="Date: jour/mois/année" formControlName="dateDebut" >
</td>
<td [formGroup]="form">
  <input type="text" placeholder="Date: jour/mois/année" formControlName="dateFin">
</td>

or like this

<div [formGroup]="form">
  // content
</div>

parent

<child-selector [form]="parentFormGroup"></child-selector>

Upvotes: 1

Related Questions