← Back to Angular, page 7

angular-0131 · Medium mid

What is the difference between setValue and patchValue, how does reset behave, and what do updateOn and the emitEvent option change in reactive forms?

setValue needs a value for every control and throws if one is missing, while patchValue updates only the controls you give. reset sets values back (to null or to given values) and clears touched and dirty. updateOn chooses when a control updates (change, blur or submit), and emitEvent false stops valueChanges from firing.

Full answer

These methods work on FormControl, FormGroup and FormArray.

setValue(value): replaces the whole value. For a group, the object must contain every control, with no extra keys, or Angular throws an error. This strictness catches typos and missing fields. Use it when you have a complete object.

patchValue(value): updates only the keys you give and ignores unknown keys. Use it for partial updates, such as filling part of a form from a server response.

reset(value?): sets the value to null (or to the initial value for non-nullable controls, or to the object you pass), and marks the control pristine and untouched. It also sets status to valid or invalid again. Use it after submit. Note that reset() on a nullable control gives null, so use nonNullable controls to get empty strings back.

valueChanges and statusChanges are observables that emit when the value or status changes. Methods take options:

  • { emitEvent: false }: do not emit valueChanges. Use it when you update a field from a valueChanges handler of another field, to avoid an endless loop.
  • { onlySelf: true }: do not update parent controls' validity.

updateOn controls when the model updates from the view: 'change' (default, on every input event), 'blur' (when the field loses focus) or 'submit' (on form submit). Set it on a control, or on the whole group (new FormGroup({...}, { updateOn: 'blur' })). It reduces noisy validation and requests, because validators and valueChanges run only at that moment.

Related points: disabled controls are excluded from form.value but included in getRawValue(). markAllAsTouched() shows all errors on a failed submit. form.dirty means the user changed something, which is useful for a "leave page?" guard.

typescript
import { FormControl, FormGroup } from '@angular/forms';

export function makeForm() {
  return new FormGroup({
    name: new FormControl('', { nonNullable: true }),
    marks: new FormControl(0, { nonNullable: true }),
  });
}

export function demo(): string[] {
  const out: string[] = [];
  const form = makeForm();

  form.patchValue({ name: 'Asha' }); // only name changes
  out.push(JSON.stringify(form.value));

  try {
    // @ts-expect-error missing "marks": setValue needs every key
    form.setValue({ name: 'Ravi' });
  } catch (error) {
    out.push('setValue threw');
  }

  form.reset();
  out.push(JSON.stringify(form.value)); // nonNullable controls return to their initial values

  const onBlur = new FormControl('', { updateOn: 'blur' });
  onBlur.setValue('typed'); // programmatic changes always apply
  out.push(onBlur.value ?? '');
  return out;
}

Angular 20.3, type-checked. The function was run in a Karma test that checks the patch result, the setValue error, the reset values, and that a programmatic change applies on an updateOn blur control.

Key points

  • `setValue` needs the full shape and throws on mismatch. `patchValue` accepts partial values.
  • `reset` clears values, touched and dirty. Use `nonNullable` to reset to empty strings.
  • `emitEvent: false` stops `valueChanges`, which prevents loops.
  • `updateOn` chooses change, blur or submit for model updates and validation.

Common mistakes

  • Using `setValue` with partial data and getting an error.
  • Calling `reset()` and getting `null` instead of an empty string.
  • Updating a control inside its own `valueChanges` without `emitEvent: false`.

Follow-ups

  • Why does `form.value` omit disabled controls?
  • How would you show all validation errors after a failed submit?
  • When would you set `updateOn: 'submit'`?

Tips

  • Explain `setValue` strictness as a feature.
  • Mention `nonNullable` and `reset`.
  • Name the loop problem and its fix.

Evaluation rubric

  • Distinguishes setValue, patchValue and reset correctly.
  • Explains updateOn and emitEvent.
  • Gives practical cautions.