Sub topic 1
Why Reactive Forms: Comparing the Two Angular Form Models
Angular has two main ways to build forms. Both come from the @angular/forms package, and both use the same classes at runtime. They differ in where the form model is defined and in who owns it. This chapter explains that difference and how each approach works internally, and gives criteria for choosing one. The rest of the tutorial uses reactive forms.
Prerequisites
This chapter assumes you already know:
- TypeScript basics, including generics and interfaces
- Angular components, templates, and data binding (
[prop],(event),[(ngModel)]) - Standalone components and the
inject()function - Basic RxJS observables and operators such as
mapanddebounceTime - HTML form elements such as
<form>,<input>, and<select>
The same form, built two ways
Here is a one-field newsletter signup written in the template-driven style. The template declares the form. Directives such as ngModel, required, and email create the controls and validators behind the scenes.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-newsletter-td',
standalone: true,
imports: [FormsModule],
template: `
<form #f="ngForm" (ngSubmit)="onSubmit()">
<input name="email" type="email" [(ngModel)]="email" required email />
<button type="submit" [disabled]="f.invalid">Subscribe</button>
</form>
`,
})
export class NewsletterTdComponent {
email = '';
onSubmit(): void {
console.log(this.email);
}
}Here is the same form in the reactive style. The component class creates the model. The template only connects inputs to controls that already exist.
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-newsletter-reactive',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<input type="email" formControlName="email" />
<button type="submit" [disabled]="form.invalid">Subscribe</button>
</form>
`,
})
export class NewsletterReactiveComponent {
form = new FormGroup({
email: new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.email],
}),
});
onSubmit(): void {
console.log(this.form.getRawValue());
}
}In both versions Angular ends up with a FormGroup that contains a FormControl named email. The difference is who creates that object and when it is created.
How each model works internally
Template-driven: the directives build the model
In template-driven forms, the NgForm directive creates a FormGroup for the <form> element. Each NgModel directive creates its own FormControl and registers it with the parent form. Your component never touches these objects directly. It holds plain properties like email, and two-way binding copies values between that property and the hidden control.
Registration is asynchronous. NgForm adds controls inside a resolved promise, so the controls do not exist yet when ngOnInit runs. If you read f.controls['email'] too early, you get undefined. Model-to-view updates are also deferred by a microtask, which is why a test often needs fixture.whenStable() before it can read the value in the input.
Reactive: the component owns the model
In reactive forms you create the FormGroup and FormControl instances yourself, synchronously, in the class. Directives such as formGroup and formControlName look up an existing control by name and attach a value accessor to the DOM element. Writes in both directions happen synchronously. The control exists from the moment the class is constructed, so it is available in the constructor, in ngOnInit, and in a plain unit test with no template at all.
Each change produces a new value object for the form instead of mutating a component property. You read the current state from the model, or subscribe to valueChanges, and you never need to copy values back by hand.
Side-by-side comparison
| Aspect | Template-driven | Reactive |
|---|---|---|
| Module | `FormsModule` | `ReactiveFormsModule` |
| Model defined in | Template, via directives | Component class |
| Model creation | Asynchronous, after view init | Synchronous, at construction |
| Data flow | Two-way binding to mutable properties | Explicit model, new value per change |
| Validators | Attribute directives (`required`, `minlength`) | Functions passed to controls |
| Static typing | Weak: values flow through `ngModel` | Strong: `FormControl<string>` inferred |
| Dynamic fields | Awkward: driven by `@for` over data | First-class: `FormArray`, `addControl` |
| Unit testing | Needs a rendered component and `whenStable` | Test the model directly |
| Boilerplate for tiny forms | Low | Slightly higher |
Decision criteria
Choose reactive forms when one or more of these is true:
- The form has many fields, nested sections, or repeating rows.
- Fields appear, disappear, or change validation based on other input.
- You need cross-field rules, server-side checks, or debounced reactions to typing.
- You want the compiler to catch typos in field names and wrong value types.
- You want to test validation logic without rendering a component.
Template-driven forms remain reasonable for small, static forms such as a login box or a search field with one or two simple validators, especially when the team already writes most logic in templates.
Common errors when choosing or switching
- Importing the wrong module.
formGroupin a template that imports onlyFormsModulefails with "Can't bind to 'formGroup' since it isn't a known property of 'form'". The next chapter covers setup. - Reading template-driven controls too early. Accessing
ngForm.controlsinngOnInitreturns an empty object because registration has not run yet. - Expecting a component property to update in reactive forms. Typing into a
formControlNameinput does not change any property you declared. Readform.valueor subscribe tovalueChanges. - Migrating halfway. Converting one field of a template-driven form to
formControlNameinside anngFormmixes the two models. Convert the whole form at once.
Exercise
A checkout form shows a "Gift message" field only when a "This is a gift" checkbox is ticked, and the message becomes required at that point. The team also wants unit tests for this rule that do not render HTML. Which form model fits better, and why?
Show answer
Reactive forms. The rule changes a validator at runtime based on another field. That is easy to do with the model in code (for example, by subscribing to the checkbox's valueChanges and updating the message control's validators). Because the model exists synchronously in the class, a test can create the FormGroup, set the checkbox value, and assert the message control's validity without a template or whenStable.
Exercise
In a template-driven form, a developer logs this.ngForm.controls['email'] inside ngOnInit and gets undefined, even though the input has name="email" and ngModel. What causes this?
Show answer
NgModel registers its FormControl with NgForm inside a resolved promise, so registration finishes after the current change detection pass. During ngOnInit the control has not been added yet. The control is available later, for example in ngAfterViewInit after a tick, or after awaiting fixture.whenStable() in tests. Reactive forms avoid this because the class creates the controls synchronously.