Template-driven forms
Building a form with ngModel directly in the template — and when that's genuinely the better choice over reactive forms.
Template-driven forms
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A template-driven form builds its model implicitly, from `ngModel` directives scattered through the template — you never explicitly construct the form's structure in TypeScript.
- `NgForm` and `NgModelGroup` let you read validity and values out of the form as a whole, even though no single place in your code defined what that whole looks like.
- This style favors simple forms with little custom validation logic; the next lesson's reactive style trades a bit of upfront ceremony for a form structure you can see and test without rendering a template at all.
Building a form by describing it in the template
The two-way binding lesson earlier in this roadmap showed [(ngModel)] as
syntax sugar for a property binding plus an event binding. A template-driven
form takes that same directive and uses it as the foundation for an entire
form’s structure — not just one input, but the form as a whole, assembled from
however many ngModel-bound fields the template happens to contain:
<form #profileForm="ngForm" (ngSubmit)="onSubmit(profileForm.value)">
<input name="name" [(ngModel)]="profile.name" required />
<input name="email" [(ngModel)]="profile.email" email required />
<button type="submit" [disabled]="profileForm.invalid">Save</button>
</form>
Nothing in the component’s TypeScript declares “this form has a name field and
an email field.” That structure exists entirely because the template put
ngModel-bound inputs there, each one registering itself with the enclosing
<form>. #profileForm="ngForm" is a template reference variable (from several
lessons back) pointing at the NgForm directive Angular automatically attaches
to any <form> element — that’s how you get a handle on the form’s aggregate
value and validity (profileForm.value, profileForm.invalid) without having
declared the form’s shape anywhere yourself.
NgModelGroup: nesting without nesting your markup’s meaning
<div ngModelGroup="address">
<input name="street" [(ngModel)]="profile.address.street" />
<input name="city" [(ngModel)]="profile.address.city" />
</div>
Real forms are rarely flat. ngModelGroup lets a template-driven form express
nested structure — here, profileForm.value.address becomes its own object
containing street and city — purely by wrapping the relevant fields in a
<div> carrying that directive. The validity of that group is independently
checkable too (profileForm.controls.address.valid), which matters the moment
you want to show “the address section specifically has an error” rather than
only a single all-or-nothing form-level validity flag.
Showing validation errors without a FormControl to query directly
Reading a field’s validity in a template-driven form means getting a template
reference variable on the ngModel directive itself, the same pattern covered
a few lessons back for reaching a component instance:
<input name="email" [(ngModel)]="profile.email" required email #email="ngModel" />
@if (email.invalid && email.touched) {
<span class="error">Enter a valid email.</span>
}
#email="ngModel" exports the directive’s own instance under that name,
giving the template access to .invalid, .touched, and .errors — the same
validity state a reactive form’s FormControl exposes directly in TypeScript,
just reached from inside the template instead, since there’s no standalone
control object elsewhere to query. .touched specifically matters for not
showing an error before the user has even interacted with the field.
Why this style, and when reactive forms earn their ceremony instead
This lesson’s video builds a complete template-driven form with validation, which is worth watching end to end to see how naturally the markup reads once you trust that Angular is assembling the form’s structure behind the scenes. That ease is real, and it’s also the honest tradeoff: because the structure lives implicitly in the template, testing a template-driven form’s logic in isolation — without rendering it — is awkward, and complex cross-field validation has nowhere clean to live except scattered custom directives. The next lesson covers reactive forms, which flip this tradeoff: more upfront code to explicitly construct the form, in exchange for a structure you can construct, inspect, and test directly in TypeScript, with no template required at all. Neither style is simply “the modern one” — template-driven forms remain a reasonable, lighter-weight choice for a short form with little custom validation logic behind it.
Check yourself
In a template-driven form, where does the actual shape of the form — which fields it has, what they're called — get defined?
Nowhere explicit in the TypeScript — it emerges implicitly from the `ngModel`/`name` attributes scattered across the template's `<input>` elements. There's no single object in your component class that lists the form's fields the way a reactive form's `FormGroup` does; Angular assembles the structure by scanning the rendered template.
What does wrapping a subset of fields in `<div ngModelGroup="address">` actually give you that plain `ngModel` on each field wouldn't?
It groups those fields under a nested `address` key in the form's overall value and validity state — so `form.value.address.street` exists as a nested object, and you can check `form.controls.address.valid` for just that group, rather than every field living flat at the form's top level.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.