Sub topic 16
Choosing When Values Update: change, blur, and submit
This chapter assumes you can build a FormGroup of FormControls, bind it with formGroup and formControlName, attach sync and async validators, and subscribe to valueChanges.
By default, a reactive control updates on every keystroke. Each input event writes the new value to the model, runs every validator, and emits on valueChanges and statusChanges. That works for most fields. It becomes costly when an async validator calls a server, or when an error message flashes while the user is still typing. The updateOn option controls when the view's value is committed to the model.
The three update modes
| `updateOn` | Model updates when | Typical use |
|---|---|---|
| `'change'` (default) | Every `input` event | Live search, instant feedback |
| `'blur'` | The input loses focus | Async checks, fields where errors shouldn't show mid-typing |
| `'submit'` | The parent form fires `ngSubmit` | Long forms validated in one pass |
Until the commit happens, the typed text stays pending inside the directive. The control's value keeps its old value, validators don't run, and valueChanges doesn't emit. The touched flag is not affected by the mode: it is still set on blur. The dirty flag is only set once the pending value is committed.
Setting updateOn on groups and controls
You can pass updateOn in the options of a FormControl, a FormGroup, or a FormArray. A group's setting is inherited by its children, and a child that sets its own updateOn overrides the inherited value.
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-signup-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<input formControlName="username" placeholder="Username" />
<input formControlName="email" placeholder="Email" />
<button type="submit">Sign up</button>
</form>
<p>Model username: {{ form.controls.username.value }}</p>
<p>Model email: {{ form.controls.email.value }}</p>
`,
})
export class SignupFormComponent {
form = new FormGroup(
{
// Inherits 'blur' from the group.
username: new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.minLength(3)],
}),
// Overrides the group and updates on every keystroke.
email: new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.email],
updateOn: 'change',
}),
},
{ updateOn: 'blur' },
);
onSubmit(): void {
console.log(this.form.getRawValue());
}
}When you run this and type into each field, the email paragraph updates live. The username paragraph changes only when you tab out of the username field.
With FormBuilder, pass the same options: fb.group({ ... }, { updateOn: 'blur' }) or fb.control('', { updateOn: 'blur' }).
Common errors
- Reading the value too early. Code that reads
control.valueduring typing gets the last committed value when the mode is'blur'or'submit'. - Using
'submit'without a form submit. Pending values are committed through theformGroupdirective'sngSubmit. Calling a method from atype="button"click doesn't commit them, so the model stays stale. - Expecting
'submit'to update the whole form at once when a child overrides it. Any control with its ownupdateOn: 'change'keeps updating live. - Calling
setValuein code. A programmaticsetValueorpatchValueupdates the model immediately, whatever the mode.updateOnonly governs values that come from the view.
Exercise
A FormGroup uses { updateOn: 'submit' }. Its child "code" control has validators [Validators.required]. The user types "X1" into the code field and clicks a <button type="button"> that calls a method logging form.valid. What is logged, and why?
Show answer
false. The typed value is still pending in the directive because a type="button" click does not fire ngSubmit. The control's value is still '', so Validators.required fails. Using a submit button, or a (ngSubmit) handler, would commit the value first.
Exercise
You want a username field to run its async availability check only after the user leaves the field, while the rest of the form gives live feedback. How do you configure it?
Show answer
Leave the group at the default 'change' and give only the username control updateOn: 'blur', for example new FormControl('', { nonNullable: true, asyncValidators: [usernameAvailable(checker)], updateOn: 'blur' }).