Reactive forms fundamentals

Intermediate · 10 min

FormControl, FormGroup, and FormBuilder — constructing a form's structure explicitly in TypeScript instead of implicitly in the template.

Before thisTemplate-driven forms

Tech Sharmit · 10 min

Reactive forms fundamentals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Tech SharmitReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A reactive form's structure is built explicitly, in TypeScript, as a tree of FormControl and FormGroup objects — the template only binds to that already-existing structure, it doesn't define it.
  • FormBuilder is a thin convenience wrapper over constructing FormControl/FormGroup instances directly by hand — both produce the identical structure underneath.
  • Because the form exists as plain objects in TypeScript, you can construct and assert against its value and validity directly in a unit test, with no template ever rendered.

Flipping where the form’s structure lives

The previous lesson’s template-driven form had no single place that declared its shape — it emerged from scattered ngModel directives across the markup. Reactive forms invert that entirely: you build the form’s structure explicitly, as plain TypeScript objects, before the template ever renders a single input.

profileForm = new FormGroup({
  name: new FormControl(''),
  email: new FormControl(''),
});
<form [formGroup]="profileForm" (ngSubmit)="onSubmit(profileForm.value)">
  <input formControlName="name" />
  <input formControlName="email" />
</form>

Read the TypeScript first, and you already know everything about this form’s shape — two fields, name and email — without needing to look at the template at all. The template’s job shrinks to simply binding to a structure that already exists: [formGroup]="profileForm" connects the <form> to the FormGroup instance, and formControlName="name" connects each input to its matching FormControl by name. Compare that to the previous lesson’s [(ngModel)]-driven form, where the template was the definition — here, the template is only a view onto a definition that lives somewhere else entirely.

FormBuilder: the same structure, less boilerplate

private fb = inject(FormBuilder);

profileForm = this.fb.group({
  name: [''],
  email: [''],
});

This produces exactly the same FormGroup/FormControl tree as the explicit new FormGroup({...}) version above — FormBuilder is a convenience layer, not a different mechanism. The array-literal shorthand (['', validators]) saves you writing new FormControl(value, validators) for every single field, which matters more than it sounds once a form has a dozen fields each carrying their own validators. Reach for FormBuilder by default; reach for constructing FormControl/FormGroup directly when you need finer control over exactly how a particular control gets built.

What building the structure explicitly actually buys you

The real payoff isn’t just where the code lives — it’s what becomes possible once a form’s structure is a plain object rather than something that only exists once a template renders. You can construct a FormGroup in a unit test, set values on it, call .updateValueAndValidity(), and assert on .valid or .value, with no TestBed, no rendered DOM, nothing beyond the form class itself. A later lesson on testing comes back to exactly this — reactive forms’ testability is one of the clearer practical arguments in their favor, alongside the structural visibility this lesson has focused on.

Reacting to changes as they happen

Because a reactive form’s controls are real objects rather than values assembled from the template, they expose their own stream of changes directly — valueChanges, an observable that emits the form’s current value every time something in it changes:

this.profileForm.valueChanges.pipe(debounceTime(300)).subscribe((value) => {
  console.log('form changed:', value);
});

This is exactly the RxJS material from earlier in this roadmap applying directly to forms — debounceTime, distinctUntilChanged, and the rest of the operator toolkit all work against valueChanges the same way they’d work against any other observable, which is the kind of composability a template-driven form’s implicit structure doesn’t offer nearly as directly.

This lesson’s video builds a FormGroup with FormBuilder directly and explains the control hierarchy as it goes; the linked “deeper” video walks through the same fundamentals on a more recent Angular version if you want a second, freshly-dated pass.

What’s next

The next lesson builds on this foundation with validation — built-in validators, writing your own, and the cross-field and dynamic-list cases that reactive forms handle more directly than template-driven forms do.

Check yourself

  1. 01In a reactive form, if you wanted to know every field a form has before ever looking at its template, where would you look?

    The FormGroup definition in the component's TypeScript — every field is explicitly listed there as a FormControl (or nested FormGroup), independent of whatever the template happens to render. A template-driven form has no equivalent single place to look; its structure only exists once the template renders.

  2. 02What's the actual difference between `new FormGroup({ name: new FormControl('') })` and `this.fb.group({ name: [''] })`?

    Nothing structural — `FormBuilder`'s `.group()` is a convenience method that constructs the identical FormGroup/FormControl tree under the hood, just with a terser array-literal syntax for each control's initial value and validators. Both produce the same object; FormBuilder just saves you writing `new FormControl(...)` explicitly for every field.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark