Angular Template-driven Forms
Learn how to create Angular forms using template-driven approach, ngModel, ngForm and template reference variables.
What are Template-driven Forms?
Template-driven forms are an Angular form approach where most of the form structure and behavior is defined directly in the HTML template.
Angular uses directives such as ngModel and ngForm to create and manage the form.
This approach is generally convenient for simple forms such as basic login, contact and enquiry forms.
Template-driven Form Flow
Main Concepts
ngForm
Represents and tracks the overall template-driven form.
ngModel
Connects a form control with Angular's form model.
Reference Variable
Provides access to form or control state inside the template.
Validation
Built-in validation attributes can be applied to controls.
Import FormsModule
Template-driven forms use Angular's FormsModule.
The module must be imported into the Angular application before using template-driven form features such as ngModel.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-enquiry',
imports: [FormsModule],
templateUrl: './enquiry.component.html'
})
export class EnquiryComponent {
}
Basic Template-driven Form
A template-driven form can be created by adding ngForm to the form element and ngModel to the controls.
Each control should have a unique name attribute so Angular can register it with the form.
<form #enquiryForm="ngForm">
<label>
Name
</label>
<input
type="text"
name="name"
ngModel>
<label>
Email
</label>
<input
type="email"
name="email"
ngModel>
<button type="submit">
Send Enquiry
</button>
</form>
Using ngModel
ngModel connects a form control with Angular's form state.
It can be used with the name attribute to register the control inside a template-driven form.
<input
type="text"
name="fullName"
ngModel>
Form Control
The fullName field is registered with the Angular form.
Using ngForm
Angular automatically creates a form directive when ngForm is applied to a form.
A template reference variable can be used to access the form's state.
<form
#courseForm="ngForm">
...
</form>
<p>
Form Valid:
{{ courseForm.valid }}
</p>
<p>
Form Submitted:
{{ courseForm.submitted }}
</p>
Two-way Binding with ngModel
Template-driven forms commonly use two-way binding with the [(ngModel)] syntax.
This allows the template control and component property to stay synchronized.
course = 'Angular';
<input
type="text"
name="course"
[(ngModel)]="course">
<p>
Selected Course:
{{ course }}
</p>
CIIT Training Institute Example
Suppose CIIT Training Institute has a course enquiry form. A visitor can enter their name, email, mobile number and preferred course.
This is a good example of a simple form that can be created using the template-driven approach.
export class EnquiryComponent {
name = '';
email = '';
mobile = '';
course = '';
}
<form
#enquiryForm="ngForm"
(ngSubmit)="submitForm(enquiryForm)">
<label>
Full Name
</label>
<input
type="text"
name="name"
[(ngModel)]="name"
required>
<label>
Email
</label>
<input
type="email"
name="email"
[(ngModel)]="email"
required>
<label>
Mobile
</label>
<input
type="text"
name="mobile"
[(ngModel)]="mobile"
required>
<label>
Preferred Course
</label>
<select
name="course"
[(ngModel)]="course"
required>
<option value="">
Select Course
</option>
<option value="Java Full Stack">
Java Full Stack
</option>
<option value=".NET Full Stack">
.NET Full Stack
</option>
<option value="Python Full Stack">
Python Full Stack
</option>
</select>
<button
type="submit"
[disabled]="enquiryForm.invalid">
Send Enquiry
</button>
</form>
submitForm(form: NgForm) {
if (form.valid) {
console.log(
form.value
);
}
}
Validation in Template-driven Forms
HTML validation attributes can be used with Angular template-driven forms.
Common validation attributes include required, email, minlength and maxlength.
<input
type="text"
name="name"
ngModel
required>
<input
type="email"
name="email"
ngModel
required
email>
<input
type="text"
name="mobile"
ngModel
minlength="10"
maxlength="10"
required>
Displaying Validation Messages
A template reference variable can be created for a control to check its validation state.
For example, a required field can display an error after the user has interacted with it.
<input
type="text"
name="name"
ngModel
required
#nameControl="ngModel">
<div
*ngIf="nameControl.invalid &&
nameControl.touched">
Name is required.
</div>
Template-driven Form States
Valid
All active validation rules are satisfied.
Invalid
One or more controls fail validation.
Touched
The user has interacted with the control.
Dirty
The control value has been changed.
Submitting a Template-driven Form
The ngSubmit event can be used to handle form submission.
The form should normally be checked for validity before its data is processed.
<form
#courseForm="ngForm"
(ngSubmit)="submitForm(courseForm)">
...
<button
type="submit"
[disabled]="courseForm.invalid">
Submit
</button>
</form>
submitForm(form: NgForm) {
if (form.valid) {
console.log(
'Form submitted',
form.value
);
}
}
Reading Form Values
The value property of the template form contains the current values of its registered controls.
This allows the submitted information to be processed by the component.
submitForm(form: NgForm) {
console.log(
form.value
);
}
Resetting a Template-driven Form
Angular provides the resetForm() method to reset the form and its state.
This can be useful after successful submission or when the user wants to clear all entered information.
resetForm(form: NgForm) {
form.resetForm();
}
Important Rules
Import FormsModule before using template-driven form features.
Registered controls should have a name attribute inside the form.
Use ngModel to connect input controls with Angular's form state.
Check the form's validity before processing or sending its values.
Advantages of Template-driven Forms
- Simple to understand for basic forms.
- Much of the form structure can be defined directly in HTML.
- Requires less explicit form-model code for simple scenarios.
- Convenient for common input fields and basic validation.
- Works well for straightforward forms with limited dynamic behavior.
Limitations of Template-driven Forms
- Complex forms can become difficult to manage in large templates.
- Dynamic form structures are generally easier to handle with reactive forms.
- Large validation requirements can make templates harder to maintain.
- Complex form logic can become closely tied to the template.
Template-driven Form Key Features
| Feature | Purpose | Example |
|---|---|---|
ngForm
|
Tracks the overall form |
#form="ngForm"
|
ngModel
|
Registers and binds controls |
ngModel
|
[(ngModel)]
|
Two-way data binding |
[(ngModel)]="name"
|
ngSubmit
|
Handles form submission |
(ngSubmit)="submitForm()"
|
required
|
Makes a field mandatory |
required
|
Template-driven Forms Best Practices
- Use template-driven forms for relatively simple form scenarios.
-
Give every registered control a meaningful
name. -
Use
[(ngModel)]when component and form values need two-way synchronization. - Show validation messages only when they are useful to the user.
- Disable submission when the form is invalid where appropriate.
- Move complex business logic out of the template.
Summary
Template-driven forms provide a simple way to create and manage Angular forms directly through the HTML template.
Important features include FormsModule, ngForm, ngModel, ngSubmit and template-based validation.
Template-driven forms are especially useful for straightforward forms such as basic enquiries, login and contact forms. In the next topic, we will learn about Reactive Forms.