Sub topic 1
Why Reactive Forms: Comparing the Two Angular Form Models
Angular ships two ways to build forms. Template-driven forms let directives in the HTML create the form model for you. Reactive forms have you create that model yourself in the component class. This chapter explains how the two differ, when each one fits, and how to import the module that reactive forms need.
Prerequisites
This chapter assumes you know:
- TypeScript basics, including generics and interfaces
- Angular components, templates, and data binding
- Standalone components and the
inject()function - Basic RxJS observables and operators
- HTML form elements
Two Ways to Own Form State
In a template-driven form, you put ngModel on inputs. Angular builds FormControl objects behind the scenes and keeps them in sync with component properties through two-way binding. The model exists, but the template owns it, and you reach it only indirectly.
In a reactive form, you write new FormControl(...) in the class. The template only connects to an object that already exists. Your code can read, change, validate, and subscribe to the form without touching the DOM.
Choosing Between Them
| Concern | Template-driven | Reactive |
|---|---|---|
| Where the model lives | Created by directives | Created in the class |
| Data flow | Asynchronous, through two-way binding | Synchronous access to the model |
| Typing | Loose | Strictly typed by default |
| Dynamic fields | Awkward | Built in with `FormArray` and `FormRecord` |
| Unit testing | Needs a rendered template | Test the model directly |
| Best fit | Small, simple forms | Complex, dynamic, or heavily validated forms |
Pick reactive forms when validation rules depend on each other, when fields are added at runtime, or when you want to test form logic without a browser. Template-driven forms still suit a short contact form or a single search field.
Importing ReactiveFormsModule
The reactive directives (formGroup, formControlName, formControl, formArrayName) are in ReactiveFormsModule from @angular/forms. A standalone component lists that module in its imports array.
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-name-field',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<label>
Name
<input [formControl]="name" />
</label>
<p>You typed: {{ name.value }}</p>
`,
})
export class NameFieldComponent {
name = new FormControl('', { nonNullable: true });
}If you forget the import, the template fails to compile with an error such as Can't bind to 'formControl' since it isn't a known property of 'input'. That message nearly always means the component is missing ReactiveFormsModule.
Exercise
A team needs an order form where users add and remove line items, quantities must be validated against stock, and the validation rules need unit tests. Which form model fits, and what must each component that uses it import?
Show answer
Reactive forms fit. Dynamic line items map to FormArray, the validation logic lives in the class, and tests can exercise the model without rendering a template. Each standalone component that binds to the form must list ReactiveFormsModule from @angular/forms in its imports array.
Exercise
You see "Can't bind to 'formControl' since it isn't a known property of 'input'". What is the most likely cause?
Show answer
The component's imports array does not include ReactiveFormsModule, so Angular does not recognize the formControl directive.