Reactive Forms vs Template-Driven Forms in Angular comparison with examples

Reactive Forms vs Template-Driven Forms in Angular.

Reactive Forms: Structured and Powerful

Example Of Reactive Form (Signup Form)

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  {
  name = 'Angular ' + VERSION.major;

  // Reactive form setup
  signupForm = new FormGroup({
    username: new FormControl('', Validators.required),
    email: new FormControl('', [Validators.required, Validators.email]),
    password: new FormControl('', [Validators.required, Validators.minLength(6)]),
    gender: new FormControl('male'),
    agree: new FormControl(false)
  });

  onSubmit() {
    console.log('Form Data:', this.signupForm.value);
  }
}
<h1>Welcome to {{ name }}</h1>
<p>Start editing to see some magic happen :)</p>

<!-- Signup Form -->
<form [formGroup]="signupForm" (ngSubmit)="onSubmit()">
  <input formControlName="username" placeholder="Username">
  <div *ngIf="signupForm.get('username')?.invalid && signupForm.get('username')?.touched">
    Username is required.
  </div>

  <input formControlName="email" placeholder="Email">
  <div *ngIf="signupForm.get('email')?.invalid && signupForm.get('email')?.touched">
    <div *ngIf="signupForm.get('email')?.errors?.required">Email is required.</div>
    <div *ngIf="signupForm.get('email')?.errors?.email">Invalid email format.</div>
  </div>

  <input type="password" formControlName="password" placeholder="Password">
  <div *ngIf="signupForm.get('password')?.invalid && signupForm.get('password')?.touched">
    <div *ngIf="signupForm.get('password')?.errors?.required">Password is required.</div>
    <div *ngIf="signupForm.get('password')?.errors?.minlength">
      Password must be at least 6 characters.
    </div>
  </div>

  <select formControlName="gender">
    <option value="male">Male</option>
    <option value="female">Female</option>
  </select>

  <label>
    <input type="checkbox" formControlName="agree"> I agree to the terms
  </label>

  <button type="submit" [disabled]="signupForm.invalid">Sign Up</button>
</form>

Why use reactive forms?

Template-Driven Forms: Simple and Easy

Example: Basic Signup Form

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'] // Make sure this is here
})
export class AppComponent {
  signupData = {
    username: '',
    email: '',
    password: ''
  };

  onSubmit(form: any) {
    console.log('Form Submitted:', this.signupData);
    form.resetForm();
  }
}

Html File

<h2>Signup Form</h2>

<form #signupForm="ngForm" (ngSubmit)="onSubmit(signupForm)">

  <!-- Username -->
  <input 
    name="username" 
    [(ngModel)]="signupData.username" 
    placeholder="Username" 
    required>
  
  <!-- Email -->
  <input 
    name="email" 
    [(ngModel)]="signupData.email" 
    placeholder="Email" 
    type="email" 
    required>
  
  <!-- Password -->
  <input 
    name="password" 
    [(ngModel)]="signupData.password" 
    placeholder="Password" 
    type="password" 
    required>
  
  <!-- Submit button -->
  <button type="submit" [disabled]="!signupForm.form.valid">Sign Up</button>

</form>

Why Use Template-Driven Forms?

Reactive Forms vs Template-Driven Forms

When to Use Reactive Forms

When to Use Template-Driven Forms

Conclusion

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *