Template-Driven And Reactive Forms In Angular

Angular is a popular web application framework that offers two approaches for building forms: Template-Driven and Reactive Forms.

Template-Driven Forms

Template-Driven Forms are a simpler approach where you create and manipulate forms using template directives. These forms are primarily suitable for simpler forms with fewer fields. You define the form elements in the HTML template, and Angular generates the corresponding form controls at runtime. You can also use directives like ngModel to bind the form controls to the components' properties. Template-driven forms are easy to understand and implement but can become hard to maintain as the form complexity grows.

Template-Driven Forms Example

app.component.html

<form #myForm="ngForm">
  <label>Name:</label>
  <input type="text" name="name" [(ngModel)]="name" required>
  <label>Email:</label>
  <input type="email" name="email" [(ngModel)]="email" required>
  <button type="submit" [disabled]="!myForm.valid">Submit</button>
</form>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  name: string;
  email: string;
}

Reactive Forms

Reactive Forms, also called Model-Driven Forms, are a more powerful approach where you define and manage forms programmatically in the component's code. Reactive forms are best suited for complex forms with many fields and more advanced validation scenarios. You define the form controls, validators, and event handlers in the component code using FormBuilder and FormControl classes. Reactive forms provide more control over the form's behavior and allow you to perform advanced operations like dynamic form creation and validation.

Overall, both approaches have pros and cons, and the choice depends on the form's complexity and the developer's preference. However, in general, reactive forms are more suitable for complex forms with advanced validation, while template-driven forms are ideal for simple forms with fewer fields.

Reactive Forms Example

app.component.html

<form [formGroup]="myForm">
  <label>Name:</label>
  <input type="text" formControlName="name">
  <label>Email:</label>
  <input type="email" formControlName="email">
  <button type="submit" [disabled]="!myForm.valid">Submit</button>
</form>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
  myForm: FormGroup;

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.myForm = this.formBuilder.group({
      name: ['', Validators.required],
      email: ['', Validators.required]
    });
  }
}

In the Template-Driven Forms example, we're using the ngForm directive to define the form structure and ngModel directive to bind form controls to component properties.

In the Reactive Forms example, we're using the FormBuilder service to define the form structure in the component using TypeScript code. We also use the formControlName directive to bind form controls to the form group.

Next Recommended Reading Angular Partial Reactive Forms