Sub topic 12
Tuning behavior with ngModelOptions
This chapter assumes you can build a standalone component that imports FormsModule, bind an input with [(ngModel)], and read control state through a template reference variable. If any of that is unfamiliar, review the earlier chapters on ngModel and NgForm first.
What ngModelOptions controls
By default, ngModel updates the control's value on every input event. It also needs a name attribute when it sits inside a <form>, because NgForm uses that name as the control's key. The ngModelOptions input lets you change this behavior for one control. It accepts an object with three useful keys:
| Option | Type | Effect | Column | Column |
|---|---|---|---|---|
| `standalone` | `boolean` | When `true`, the control is not registered with the parent form. No `name` is required. | ||
| `name` | `string` | Sets the control name without using the `name` attribute. | ||
| `updateOn` | `'change' \ | 'blur' \ | 'submit'` | Chooses when the control's value and validity are updated. |
You pass the object as a property binding:
<input [(ngModel)]="model.name" [ngModelOptions]="{ updateOn: 'blur' }" name="name" />Delaying updates with updateOn
The default is updateOn: 'change', which means every keystroke updates the model and re-runs validators. That is often fine. It becomes a problem when a validator is expensive, or when error messages flicker while the user is still typing.
Setting updateOn: 'blur' waits until the input loses focus. Setting updateOn: 'submit' waits until the form is submitted.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-profile',
imports: [FormsModule],
template: `
<form #f="ngForm" (ngSubmit)="onSubmit()">
<label for="email">Email</label>
<input
id="email"
name="email"
type="email"
[(ngModel)]="model.email"
[ngModelOptions]="{ updateOn: 'blur' }"
#email="ngModel"
required
email
/>
@if (email.invalid && email.touched) {
<p role="alert">Enter a valid email address.</p>
}
<p>Model value: {{ model.email }}</p>
<button type="submit" [disabled]="f.invalid">Save</button>
</form>
`,
})
export class ProfileComponent {
model = { email: '' };
onSubmit() {
console.log('Submitted', this.model);
}
}While you type, the "Model value" line stays empty. It updates when you tab out of the field. Validation runs at the same moment, so the error does not appear mid-word.
Setting a form-wide default
Repeating updateOn on every input is tedious. You can set a default on the form itself and let controls override it:
<form #f="ngForm" [ngFormOptions]="{ updateOn: 'blur' }">
<input name="first" [(ngModel)]="model.first" />
<input name="last" [(ngModel)]="model.last" [ngModelOptions]="{ updateOn: 'change' }" />
</form>Here first updates on blur, while last keeps the per-keystroke behavior because its own option wins.
Standalone controls
Sometimes an input lives inside a <form> but should not be part of it. A search box in a form-wrapped toolbar is a common case. Without help, Angular throws an error saying the control needs a name attribute or standalone: true.
<form #f="ngForm">
<input name="title" [(ngModel)]="model.title" required />
<input [(ngModel)]="searchTerm" [ngModelOptions]="{ standalone: true }" placeholder="Search" />
</form>The search input still binds two-way, but f.value contains only title, and the search input does not affect f.valid.
Common errors
- Missing name. An
ngModelinside a form with nonameand nostandalone: truefails at runtime. Add one or the other. - Expecting live values with
blur. WithupdateOn: 'blur', the model,{{ }}output, and validity all lag until blur. Do not use it where other UI must react to each keystroke. - Submitting stale values with
submit. WithupdateOn: 'submit',invalidstays false until the first submit. Do not usef.invalidto disable the button in that case, because it would not update as the user types. - Confusing
namein options with the attribute. Use[ngModelOptions]="{ name: 'x' }"only when you cannot set the attribute. Otherwise the attribute is clearer.
Exercise
A search input inside a <form> uses [(ngModel)]="query" and has no name attribute. Angular reports an error. Give two ways to fix it, and say how each changes f.value.
Show answer
Add name="query", which registers the control and puts query in f.value. Or add [ngModelOptions]="{ standalone: true }", which keeps the control out of the form, so f.value does not include it and it does not affect f.valid.
Exercise
A form has [ngFormOptions]="{ updateOn: 'submit' }". A Save button uses [disabled]="f.invalid". Why might the button stay enabled while the user types invalid data?
Show answer
With updateOn: 'submit', values and validity are not pushed to the control until submit, so f.invalid does not change as the user types. Use blur or change for fields that should drive the button state.