Sub topic 5
Binding the Model to the Template
This chapter assumes you know Angular components and templates, property and event binding, and standalone component imports. It also assumes you have read the earlier chapters on FormControl and FormGroup. Here you connect those model objects to real HTML inputs. You will learn how the binding directives work, how data moves in each direction, and which errors you are most likely to hit.
The Directives at a Glance
With reactive forms, you build the form model in the component class. The template only points at parts of that model. ReactiveFormsModule provides a small set of directives for this.
| Directive | Binds to | Typical use |
|---|---|---|
| `[formGroup]` | A `FormGroup` instance | The `<form>` element or a wrapper element |
| `formControlName` | A child control of the nearest parent group, looked up by name | Inputs inside a `[formGroup]` |
| `[formControl]` | A `FormControl` instance, passed directly | A standalone control with no parent group |
| `formGroupName` | A nested group, looked up by name | Covered in the nested groups chapter |
| `formArrayName` | A `FormArray`, looked up by name | Covered in the FormArray chapter |
The difference that matters most is how each directive receives its control. [formGroup] and [formControl] use property binding, so you pass an object reference. formControlName is a plain string attribute that names a key. That key is resolved against the closest parent group directive.
A Complete Example
The component below binds a group to a form and also binds one standalone control outside it.
import { Component } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-shipping-form',
standalone: true,
imports: [ReactiveFormsModule, JsonPipe],
template: `
<form [formGroup]="shipping" (ngSubmit)="save()">
<label>
Full name
<input formControlName="fullName" />
</label>
<label>
City
<input formControlName="city" />
</label>
<label>
Country
<select formControlName="country">
<option value="DE">Germany</option>
<option value="FR">France</option>
</select>
</label>
<label>
<input type="checkbox" formControlName="express" />
Express delivery
</label>
<button type="submit">Save</button>
</form>
<label>
Delivery notes
<input [formControl]="notes" />
</label>
<pre>{{ shipping.value | json }}</pre>
<p>Notes: {{ notes.value }}</p>
`,
})
export class ShippingFormComponent {
shipping = new FormGroup({
fullName: new FormControl('', { nonNullable: true }),
city: new FormControl('', { nonNullable: true }),
country: new FormControl('DE', { nonNullable: true }),
express: new FormControl(false, { nonNullable: true }),
});
notes = new FormControl('', { nonNullable: true });
save(): void {
console.log('shipping', this.shipping.value, 'notes', this.notes.value);
}
}Each control has the right value type for its element. A text input holds a string. The <select> holds the value of the chosen option. The checkbox holds a boolean. Angular picks a matching value accessor for each element automatically, which is why the checkbox gives you true or false and not the string "on".
The form uses (ngSubmit) instead of (submit). The formGroup directive emits ngSubmit after it marks the group as submitted. It also calls preventDefault() on the native submit event, so the page does not reload.
How Data Flows Through the Binding
Every binding directive connects a control to a DOM element through a value accessor. A value accessor is an object that knows how to write a value to one kind of element and how to listen for user changes on it. The default accessor for text inputs writes to the element's value property and listens to input events.
Two details in this flow matter in practice.
- View to model is synchronous. When the user types, the control's value updates right away, before change detection runs. Code that reads
shipping.valuein an event handler always sees the latest input. - Model to view skips the loop. When the accessor pushes a user change into the control, it passes
emitModelToViewChange: false. That keeps the control from writing the same value back to the element, which would reset the cursor position.
Because the model is the source of truth, you never bind [value] on these inputs yourself. If you set a value in code, the element follows it.
Resolution Rules for formControlName
formControlName="city" asks the nearest parent ControlContainer for a child named city. A ControlContainer is the base class behind formGroup, formGroupName, and formArrayName. Angular resolves the name through dependency injection, so the parent has to be an ancestor element in the same component template. A group bound in a parent component is not visible inside a child component's template unless you pass it down explicitly.
[formControl] skips this lookup. You can bind it to any control reference, including one that belongs to a group:
<input [formControl]="shipping.controls.city" />This works without any [formGroup] ancestor. It is handy when a child component receives a single control as an @Input().
Common Errors and What They Mean
| Error message (abbreviated) | Cause | Fix |
|---|---|---|
| `Can't bind to 'formGroup' since it isn't a known property of 'form'` | `ReactiveFormsModule` is missing from the component's `imports` | Add it to `imports` |
| `formControlName must be used with a parent formGroup directive` | No `[formGroup]` ancestor in this template | Wrap the input, or use `[formControl]` |
| `Cannot find control with name: 'citty'` | The string does not match a key in the group | Fix the typo. Strict typing does not check template strings unless you enable strict template checks |
| `formGroup expects a FormGroup instance` | The bound expression is `undefined` at first render | Create the group in a field initializer or the constructor, not in an async callback |
| `No value accessor for form control name: 'x'` | The element is not a form element and has no accessor | Use an input, or implement `ControlValueAccessor` (covered in a later chapter) |
Choosing Between formControlName and [formControl]
Use formControlName inside a group when the template layout follows the model structure. The markup stays short, and it updates automatically if the group swaps one control for another under the same key. Use [formControl] for standalone controls such as a search box, or when you pass a single control into a reusable child component. Do not create controls inside the template expression, as in [formControl]="new FormControl()". That syntax is not allowed in templates, and the user's input would have nowhere to persist.
Exercise
A developer moves the City input into a child component, <app-city-field>, and keeps formControlName="city" inside the child's template. The app throws "formControlName must be used with a parent formGroup directive". Why, and what is the simplest fix?
Show answer
formControlName finds its parent group through dependency injection on ancestor elements in the same template. The child component's template has no formGroup ancestor, so the lookup fails. The simplest fix is to pass the control in as an input, for example [control]="shipping.controls.city", and bind it inside the child with [formControl]="control".
Exercise
In the shipping example, the user checks the Express delivery box and then clicks Save. What value does shipping.value.express hold inside save(), and why is it not the string "on"?
Show answer
It holds the boolean true. For checkbox inputs, Angular uses CheckboxControlValueAccessor, which reads the element's checked property instead of its value attribute. That accessor is updated synchronously on the change event, so the value is already current when ngSubmit fires.