Sub topic 5
Setting, Patching, and Resetting Values
This chapter assumes you can create a FormGroup of FormControl instances and bind it with formGroup and formControlName. Here you change the model from code. You will do this when you load saved data, fill in defaults, or clear the form after a submit.
The three methods
Every control, group, and array has the same three methods for writing values. They differ in how strict they are about the shape of the value you pass.
| Method | Accepts | Missing keys | Unknown keys | Also changes state |
|---|---|---|---|---|
| `setValue(v)` | The complete value | Throws an error | Throws an error | No |
| `patchValue(v)` | Any subset | Ignored | Ignored | No |
| `reset(v?)` | Optional value | Reset to default | Ignored | Marks pristine and untouched |
On a single FormControl, setValue and patchValue do the same thing. The difference only shows up on groups and arrays.
A worked example
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-account-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form">
<input formControlName="name" placeholder="Name" />
<input formControlName="city" placeholder="City" />
</form>
<button type="button" (click)="load()">Load saved</button>
<button type="button" (click)="moveCity()">Change city</button>
<button type="button" (click)="clear()">Reset</button>
<pre>{{ form.value | json }} dirty: {{ form.dirty }}</pre>
`,
})
export class AccountFormComponent {
form = new FormGroup({
name: new FormControl('', { nonNullable: true }),
city: new FormControl('Lisbon', { nonNullable: true }),
});
load(): void {
// Every key is required; leaving one out throws at runtime.
this.form.setValue({ name: 'Ana', city: 'Porto' });
}
moveCity(): void {
// Only the keys you pass are touched.
this.form.patchValue({ city: 'Braga' });
}
clear(): void {
// Non-nullable controls return to their initial values.
this.form.reset();
}
}To run the template as shown, add JsonPipe from @angular/common to imports.
With strictly typed forms, setValue({ name: 'Ana' }) fails at compile time because city is missing. patchValue accepts Partial of the value type, so the same call compiles.
Choosing between them
- Use
setValuewhen you have a complete object, such as a record from an API. Its strictness catches backend changes and typos early. - Use
patchValuewhen you have partial data, or when the data source has extra fields such asidorcreatedAtthat the form does not model. - Use
resetafter a successful submit, or when the user cancels edits.
What reset really does
reset() does more than clear values. It marks every control as pristine and untouched, so any error messages gated on touched or dirty disappear. The value each control returns to depends on how you created it:
- A control created with
nonNullable: truereturns to its initial value. - A nullable control becomes
null.
You can also pass a value: form.reset({ name: 'Guest' }) resets the state and sets name at once. Any key you leave out falls back to its default.
Silencing events
All three methods accept an options object. { emitEvent: false } updates the value without firing valueChanges or statusChanges. This helps when a subscriber writes back into the form and would otherwise loop. { onlySelf: true } stops the parent group from recalculating its value and validity.
this.form.controls.city.setValue('Faro', { emitEvent: false });Exercise
An API returns { id: 7, name: 'Ana', city: 'Porto' } and your form has only name and city controls. Which method loads it without an error, and what happens to id?
Show answer
patchValue. It sets name and city and silently ignores id. setValue would throw because id has no matching control.
Exercise
A form has a nullable control created with new FormControl('draft'). What is its value after form.reset()?
Show answer
null. Only controls created with nonNullable: true return to their initial value on reset.