Skip to main content
Angular Basics
CHAPTER 12 Beginner

Template-Driven Forms

Updated: May 18, 2026
5 min read

# CHAPTER 12

Template-Driven Forms

1. Chapter Introduction

Template-Driven Forms let you build forms primarily in HTML using Angular directives. The ngModel directive synchronizes form inputs with TypeScript properties automatically. While less powerful than Reactive Forms, they are quick to implement for straightforward scenarios like login, contact, and simple registration forms.

2. Learning Objectives

  • Use ngModel for two-way binding in forms.
  • Use required, minlength, email HTML validation attributes.
  • Access the form's validation state via template reference variables.
  • Display user-friendly error messages.
  • Handle form submission.

3. Basic Template-Driven Form

register.component.ts
12345678910111213141516
import { Component } from '@angular/core';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html'
})
export class RegisterComponent {
  name: string = '';
  email: string = '';
  password: string = '';

  onSubmit(): void {
    console.log({ name: this.name, email: this.email });
    alert('Registration successful!');
  }
}
html
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
<!-- register.component.html -->
<form #registerForm="ngForm" (ngSubmit)="onSubmit()">

  <div>
    <label>Full Name</label>
    <input
      type="text"
      name="name"
      [(ngModel)]="name"
      required
      minlength="3"
      #nameField="ngModel"
    />
    <div *ngIf="nameField.invalid && nameField.touched">
      <span *ngIf="nameField.errors?.[&#039;required']">Name is required.</span>
      <span *ngIf="nameField.errors?.[&#039;minlength']">Min 3 characters.</span>
    </div>
  </div>

  <div>
    <label>Email</label>
    <input
      type="email"
      name="email"
      [(ngModel)]="email"
      required
      email
      #emailField="ngModel"
    />
    <div *ngIf="emailField.invalid && emailField.touched">
      <span *ngIf="emailField.errors?.[&#039;required']">Email is required.</span>
      <span *ngIf="emailField.errors?.[&#039;email']">Enter a valid email.</span>
    </div>
  </div>

  <div>
    <label>Password</label>
    <input
      type="password"
      name="password"
      [(ngModel)]="password"
      required
      minlength="6"
      #passwordField="ngModel"
    />
    <div *ngIf="passwordField.invalid && passwordField.touched">
      <span *ngIf="passwordField.errors?.[&#039;required']">Password is required.</span>
      <span *ngIf="passwordField.errors?.[&#039;minlength']">Min 6 characters.</span>
    </div>
  </div>

  <!-- Disabled until form is valid -->
  <button type="submit" [disabled]="registerForm.invalid">Register</button>

</form>

4. Template Reference Variables

#registerForm="ngForm" creates a variable referencing the form's NgForm object. This gives you access to the overall form state in the template:
  • registerForm.valid / registerForm.invalid
  • registerForm.submitted
  • registerForm.value (all form values as an object)

Similarly, #nameField="ngModel" gives access to the individual control's state.

5. Styling Validation States

Angular automatically adds CSS classes to inputs based on their state:
  • .ng-valid / .ng-invalid
  • .ng-touched / .ng-untouched
  • .ng-dirty / .ng-pristine
css
123456
input.ng-invalid.ng-touched {
  border: 2px solid red;
}
input.ng-valid.ng-touched {
  border: 2px solid green;
}

6. Resetting the Form

typescript
1234567891011
import { ViewChild } from &#039;@angular/core&#039;;
import { NgForm } from &#039;@angular/forms&#039;;

export class RegisterComponent {
  @ViewChild(&#039;registerForm&#039;) form!: NgForm;

  onSubmit(): void {
    console.log(this.form.value);
    this.form.reset(); // Reset all values and validation states
  }
}

7. Common Mistakes

  • Missing name attribute: Every input in a Template-Driven form MUST have a name attribute. Without it, ngModel cannot register the control and the form submission will not include that field.
  • Not importing FormsModule: Template-driven forms require FormsModule in the imports array of your module or standalone component.

8. Mini Project: Registration Form

Build a complete user registration form with Name, Email, Password, Confirm Password fields, all with validation, error messages, and styled borders.

9. MCQs with Answers

Question 1

What directive enables two-way binding for form inputs in Template-Driven Forms?

Question 2

What is the purpose of a template reference variable like #emailField="ngModel"?

Question 3

What attribute makes an input field mandatory in a Template-Driven form?

Question 4

The #myForm="ngForm" syntax exposes the form's NgForm object. Which property tells you if the entire form is valid?

Question 5

What CSS class does Angular automatically add to an input that has been interacted with but failed validation?

Question 6

Which event on the <form> tag is used to handle submission in Template-Driven forms?

Question 7

What is REQUIRED on every <input> element in a Template-Driven form for ngModel to register it?

Question 8

Which module must be imported to use Template-Driven Forms?

Question 9

How do you disable the submit button until the form is valid?

Question 10

What method resets all form fields and their validation states?

10. Interview Questions

  • Q: What is the role of #fieldName="ngModel" in a Template-Driven form?
  • Q: Explain how Angular's automatic CSS classes (ng-valid, ng-invalid) help with UX design.

11. Summary

Template-Driven Forms are Angular's quickest path to a working form. By leveraging ngModel, template reference variables, and HTML validation attributes, you can build fully validated forms with minimal TypeScript code. The automatic CSS class system and touched/dirty states provide a polished, user-friendly experience out of the box.

12. Next Chapter Recommendation

For complex, dynamic forms with programmatic control, Chapter 13: Reactive Forms introduces FormBuilder, FormGroup, and custom validators — giving you full TypeScript control over every aspect of your forms.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·