Sub topic 4
Binding the Model to the Template
This chapter assumes you already know how to create a FormControl and a FormGroup in component code, and that you understand standalone components and Angular property binding. Here you connect that model to real <input> elements.
The three binding directives
ReactiveFormsModule gives you three main directives for connecting the model to the DOM:
| Directive | Binds | Typical use |
|---|---|---|
| `[formGroup]` | A `FormGroup` instance to a `<form>` or container element | The root of a form |
| `formControlName` | A child control, looked up by its key in the parent group | Inputs inside a `[formGroup]` |
| `[formControl]` | A standalone `FormControl` instance | A single input with no group, such as a search box |
The model stays the source of truth. The directives keep the DOM value and the control value in sync in both directions. When the user types, the control updates. When code changes the control, the input updates.
A working example
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-contact-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()">
<label>
Name
<input formControlName="name" />
</label>
<label>
Email
<input type="email" formControlName="email" />
</label>
<button type="submit">Send</button>
</form>
<label>
Filter
<input [formControl]="filter" />
</label>
<p>Filter value: {{ filter.value }}</p>
`,
})
export class ContactFormComponent {
contactForm = new FormGroup({
name: new FormControl('', { nonNullable: true }),
email: new FormControl('', { nonNullable: true }),
});
filter = new FormControl('', { nonNullable: true });
onSubmit(): void {
console.log(this.contactForm.value);
}
}Look at three things in this example:
formControlName="name"is a plain string attribute. It is not a property binding. Angular finds the nearest parent[formGroup]and looks up the keynamein it.[formControl]="filter"uses square brackets because it receives the control object itself.(ngSubmit)fires when the form is submitted. Angular stops the browser's default page reload for you.
Common errors
| Error message | Cause | Fix |
|---|---|---|
| `Can't bind to 'formGroup' since it isn't a known property of 'form'` | `ReactiveFormsModule` is missing from `imports` | Add it to the component's `imports` array |
| `formControlName must be used with a parent formGroup directive` | The input sits outside any `[formGroup]` element | Move it inside the form, or switch to `[formControl]` |
| `Cannot find control with name: 'emial'` | The attribute string doesn't match any key in the group | Fix the spelling so it matches the model key exactly |
Exercise
A component defines loginForm = new FormGroup({ user: new FormControl(''), pass: new FormControl('') }). The template contains <form [formGroup]="loginForm"><input formControlName="username" /></form>. What happens at runtime, and how do you fix it?
Show answer
Angular throws "Cannot find control with name: 'username'" because the group has no key called username. Change the attribute to formControlName="user", or rename the key in the model to username.
Exercise
You need a single "Search" input in a toolbar, and it is not part of any form. Which directive do you use, and do you write it with square brackets?
Show answer
Use [formControl] with square brackets, for example <input [formControl]="search" />, where search is a FormControl property on the component. formControlName won't work here because it needs a parent [formGroup].