Template-Driven Forms — Angular

A template-driven form is a form whose rules are written in the template. FormsModule puts NgForm (the directive that holds the whole form) on every <form>…

For a login form with two fields, building a FormGroup and getters in the class feels heavy. There's a lighter way for small forms.

In a template-driven form, the rules are written right in the template as HTML attributes: required, pattern, min. You don't create a FormGroup yourself. Angular reads the template and builds the controls for you. Picture a delivery form at Jali's Pancake Stall whose rules are printed on the paper itself: "required" under the name field, "starts with 07" under the phone field. The cashier needs no rule book, because they read the rules from the paper. But a field with no label on it doesn't get counted at all. In Angular, the paper is the template, the printed rules are attributes like required, and a field's label is its name attribute.

ngForm and ngModel

This is the same order form as in the Reactive Forms lesson, written the template-driven way:

import { Component } from '@angular/core'
import { FormsModule, NgForm } from '@angular/forms'

@Component({
  selector: 'app-order',
  standalone: true,
  imports: [FormsModule],
  template: `
    <form #f="ngForm" (ngSubmit)="send(f)">
      <input name="name" ngModel required placeholder="Name" #name="ngModel" />
      @if (name.touched && name.errors?.['required']) {
        <p class="error">Name is required.</p>
      }
      <input name="phone" ngModel required pattern="07[0-9]{9}" placeholder="Phone number" #phone="ngModel" />
      @if (phone.touched && phone.errors?.['required']) {
        <p class="error">Phone number is required.</p>
      }
      @if (phone.touched && phone.errors?.['pattern']) {
        <p class="error">The phone number starts with 07, 11 digits in all.</p>
      }
      <input name="quantity" type="number" [ngModel]="1" required min="1" #quantity="ngModel" />
      @if (quantity.touched && quantity.errors) {
        <p class="error">Quantity must be at least 1.</p>
      }
      <button type="submit" [disabled]="f.invalid">Send</button>
    </form>
  `,
})
export class Order {
  send(f: NgForm) {
    console.log('sent:', JSON.stringify(f.value))
  }
}

name: without it the field isn't registered

NgForm registers each field under the name from its name attribute. A phone field with no name can't be registered. Angular refuses it, and the console writes an error that starts with:

NG01352: If ngModel is used within a form tag, either the name attribute must be set or the form
    control must be defined as 'standalone' in ngModelOptions.

The page still shows, but the phone field stands on its own, outside the form's count:

With name="phone" Without name
f.value (name Sinta, phone empty) {"name":"Sinta","phone":"","quantity":1} {"name":"Sinta","quantity":1}
required on the phone field counted not counted
f.invalid true, Send is off false, Send is on

So the button is on even though the phone field is empty, and the order can be sent with no phone number. Every ngModel inside a <form> needs a name. A field that really isn't part of the form (for example a search box) gets [ngModelOptions]="{ standalone: true }".

ngModel, [ngModel] or [(ngModel)]

You can write ngModel in three ways:

Written Initial value Typing is stored in
ngModel empty f.value only
[ngModel]="1" 1 f.value only
[(ngModel)]="order.phone" what order.phone holds f.value and order.phone in the class

Two-way binding from the Two-Way Binding lesson still works inside a form, as long as there's a name. <input name="phone" [(ngModel)]="order.phone" /> fills the order object in the class, and the field stays registered in f.

Reactive or template-driven?

Reactive Template-driven Signal Forms
Controls are created in the class: new FormControl(...) the template: ngModel + name the class: form(signal({...}))
The rules Validators.required, Validators.pattern(...) the attributes required, pattern="..." the functions required(o.phone), pattern(o.phone, ...)
Module ReactiveFormsModule FormsModule FormField from @angular/forms/signals
Tested without the DOM yes needs its template shown on a page yes, inside an injection context
Good for big forms, fields that get added, rules that depend on each other small forms: login, a search box, one or two fields new apps that already use signals

An injection context is a place where inject() may be called, for example a constructor. Reactive and template-driven forms give the same errors, touched and valid. Pick one way per form, and don't mix them.

The third column, Signal Forms, has been stable since Angular 22, and the Reactive Forms lesson has a full example. In Signal Forms, the same state is read through signals: errors(), touched() and valid().

The exercise uses the smallest shape: <form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">, one <input name="email" ngModel required /> and <button [disabled]="myForm.invalid">Submit</button>.

⚠️ Fields that don't get counted

TL;DR: A template-driven form is a form whose rules are written in the template. FormsModule puts NgForm (the directive that holds the whole form) on every <form> automatically, and #f="ngForm" makes f point to that NgForm, so f.value and f.invalid can be read. Each field is registered with ngModel plus name, and the rules are attributes like required and pattern. A field with no name inside a form triggers the error NG01352 and isn't counted, so the form can be sent with that field empty. #phone="ngModel" gives phone.errors and phone.touched for error messages. Reactive forms suit big forms that need testing without the DOM, and template-driven ones suit small forms.

⚡ The rules are written in the template

Write:

<form #f="ngForm" (ngSubmit)="send(f)">
  <input name="phone" ngModel required pattern="07[0-9]{9}" #phone="ngModel" />
  <button type="submit" [disabled]="f.invalid">Send</button>
</form>

Meaning: In a template-driven form, the rules are written in the template. FormsModule puts NgForm (the directive that holds the whole form) on every <form> automatically. #f="ngForm" makes f point to that NgForm, so f.value and f.invalid can be read. ngModel plus name="phone" registers that field with the form. The rules are plain attributes: required, pattern, min. #phone="ngModel" gives phone.errors and phone.touched for error messages.

In the exercise, the grader checks for #myForm="ngForm", ngModel, and an <input> with name="email" and required inside the <form>.

⚡ An ngModel inside a form needs a name

Write:

<input name="phone" ngModel required />
Guess first: the phone field has no name and is still empty. Is the Send button off or on?

On. A field with no name isn't registered with the form, so its required isn't counted and f.invalid stays false. The console writes NG01352, and the order can be sent with no phone number.

Meaning: NgForm registers each field under the name from its name attribute. Every ngModel inside a <form> needs a name.

⚡ Reactive or template-driven?

Meaning: Reactive forms (their controls are created in the class) and template-driven forms give the same errors, touched and valid. Reactive forms suit big forms that need testing without the DOM, and template-driven ones suit small forms like a login or a search box. Pick one way per form, and don't mix them.

What you will learn