Have you heard about Hack Frontend Community?Join us on Telegram!
Practice React Problems

Template-driven vs Reactive Forms in Angular

What are Forms in Angular?

In Angular there are two approaches to creating forms:

  1. Template-driven forms — forms built based on templates (HTML).
  2. Reactive forms — forms managed from TypeScript code.

Comparison Table

CharacteristicTemplate-driven FormsReactive Forms
FoundationHTML templateTypeScript code
ModuleFormsModuleReactiveFormsModule
ManagementDeclarative (via template)Imperative (via code)
ApproachLight, declarativeFlexible, programmatic
ValidationVia HTML attributes + directivesVia API and methods
TestingMore complexEasier
Suitable forSimple formsComplex/dynamic forms

Template-driven Form Example

<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
  <input name="username" ngModel required />
  <input name="email" ngModel email />
  <button type="submit">Submit</button>
</form>
onSubmit(form: NgForm) {
  console.log(form.value);
}
  • Uses ngModel for data binding.
  • Directives (required, email) for validation.
  • Logic is in template.

Reactive Form Example

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

form = new FormGroup({
  username: new FormControl('', Validators.required),
  email: new FormControl('', [Validators.required, Validators.email])
});

onSubmit() {
  console.log(this.form.value);
}
<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <input formControlName="username" />
  <input formControlName="email" />
  <button type="submit">Submit</button>
</form>
  • Management completely in TS code.
  • Validation set in FormControl via Validators.
  • Better suited for complex scenarios.

When to Use?

ScenarioRecommended approach
Simple login formTemplate-driven
Complex dynamic form with logic and validationReactive
Need for scaling and testingReactive
Quick and simple form implementationTemplate-driven

Important:

Both approaches can be combined, but better choose one and stick with it throughout project for consistency.

Practice React Problems

By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.