Sub topic 6
Setting, Patching, and Resetting Values
This chapter assumes you can already create a FormGroup of FormControl instances and bind it to a template with formGroup and formControlName. It also assumes basic TypeScript and RxJS. Angular gives you three methods for writing values into a form model from code: setValue, patchValue, and reset. They look alike but follow different rules about completeness, form state, and events. Choosing the wrong one causes most of the bugs people hit when loading or clearing a form.
The three methods at a glance
| Method | Needs every key? | Unknown keys | Changes touched/dirty | Typical use |
|---|---|---|---|---|
| `setValue` | Yes, for groups and arrays | Throws an error | No | Replacing the whole model with a complete object |
| `patchValue` | No | Ignored | No | Loading partial data, such as an API response |
| `reset` | No | Ignored | Marks pristine and untouched | Clearing after submit, or loading a new baseline |
setValue: strict and complete
On a single FormControl, setValue replaces the value. On a FormGroup or FormArray, it requires a value for every child. A missing key throws Must supply a value for form control with name: 'x', and an extra key throws Cannot find form control with name: 'x'. With typed forms, the compiler also rejects an incomplete object before the code runs.
That strictness is useful. If a teammate adds a control to the form and forgets to update the code that fills it, setValue fails loudly instead of silently leaving the new field empty.
patchValue: forgiving and partial
patchValue walks the object you pass and updates only the controls whose keys match. It skips missing keys and ignores unknown keys. Use it when the data source does not line up exactly with the form, for example an API object with extra fields such as id or createdAt.
The forgiving behavior has a cost. A typo in a key name does nothing at all, with no error. Typed forms catch some of these mistakes, but only when the object literal is written inline.
reset: values plus state
reset does two things. It writes values, and it marks every control pristine and untouched. Without an argument, each control returns to its default value. For a control created with nonNullable: true, the default is the initial value. For a nullable control, the default is null.
When you pass a value, reset uses it for the matching controls. Controls missing from the object are reset to their defaults, so reset does not keep their current values the way patchValue does. Passing a value does not change the default, so a later reset() with no argument still returns to the original initial values.
A working example
The component below loads a profile, lets the user edit it, and demonstrates all three methods.
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';
interface ProfileDto {
id: number;
name: string;
email: string;
createdAt: string;
}
@Component({
selector: 'app-profile-editor',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="save()">
<input formControlName="name" placeholder="Name" />
<input formControlName="email" placeholder="Email" />
<input formControlName="nickname" placeholder="Nickname" />
<button type="submit">Save</button>
<button type="button" (click)="fillAll()">Fill all (setValue)</button>
<button type="button" (click)="clearForm()">Clear (reset)</button>
</form>
<p>Dirty: {{ form.dirty }} | Touched: {{ form.touched }}</p>
`,
})
export class ProfileEditorComponent {
form = new FormGroup({
name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
email: new FormControl('', { nonNullable: true, validators: [Validators.email] }),
nickname: new FormControl<string | null>(null),
});
constructor() {
// Simulated API call returning extra fields the form does not have.
of<ProfileDto>({ id: 7, name: 'Ada', email: '[email protected]', createdAt: '2026-01-01' })
.pipe(delay(500))
.subscribe((dto) => {
// patchValue ignores id and createdAt and leaves nickname alone.
this.form.patchValue(dto);
});
}
fillAll(): void {
// setValue requires all three keys. Removing one fails to compile.
this.form.setValue({ name: 'Grace', email: '[email protected]', nickname: 'Amazing' });
}
clearForm(): void {
// name and email return to '', nickname returns to null.
this.form.reset();
}
save(): void {
console.log('saving', this.form.getRawValue());
// Make the saved values the new clean state.
this.form.reset(this.form.getRawValue());
}
}Look at the last line of save. Calling reset with the current values keeps what the user typed and marks the form pristine. A "you have unsaved changes" check based on form.dirty then works correctly after saving.
Event and propagation options
All three methods accept an options object as the second argument:
emitEvent: falsestopsvalueChangesandstatusChangesfrom firing. Use it when writing values inside avalueChangessubscription, or you can trigger an infinite loop.onlySelf: trueupdates this control without recalculating the value and validity of its parents. This is rarely needed, and it can leave a parent group showing a stalevalidstatus.
this.form.patchValue({ name: 'Ada' }, { emitEvent: false });Validation still runs when emitEvent is false. Only the observable notifications are skipped.
FormArray edge cases
Arrays follow the same rules by index. setValue on a FormArray requires exactly as many items as the array has controls. patchValue updates the indexes that exist and ignores extra items. Neither method adds or removes controls, and neither does reset.
Common mistakes
- Calling
setValuewith an API response. Extra fields throw at runtime when the object is typed loosely. - Calling
reset({ name: 'x' })and expecting the other fields to keep their values. They return to their defaults. - Expecting
reset()to return to the last loaded data. It returns to the construction-time defaults unless you pass values. - Expecting
reset()to clear a nullable control to''. It becomesnull.
Exercise
A form has nonNullable controls title (initial value 'Draft') and body (initial value ''). The user types 'Hello' into title and 'World' into body. You then call form.reset({ body: 'Saved' }). What are the values of title and body, and is the form dirty?
Show answer
title is 'Draft' because a key missing from the reset object goes back to its default, which is the initial value for a nonNullable control. body is 'Saved'. The form is not dirty, because reset marks every control pristine and untouched.
Exercise
You receive { id: 3, sku: 'A1', qty: 2 } from a server and want to load it into a group with controls sku and qty. Which method should you use, and why would the other value-writing method fail?
Show answer
Use patchValue, which ignores the extra id key. setValue would throw "Cannot find form control with name: 'id'" because it rejects keys that have no matching control. With typed forms, the compiler may also reject the object.
Exercise
Inside form.controls.country.valueChanges.subscribe(...) you call form.controls.region.setValue(''), and region has its own valueChanges subscription that sets country. How do you prevent the two subscriptions from triggering each other forever?
Show answer
Pass { emitEvent: false } when writing from inside a subscription, for example form.controls.region.setValue('', { emitEvent: false }). The value and validity still update, but valueChanges does not fire, so the loop never starts.