angular
/

Angular Template-Driven Forms – Simple & Declarative

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Template-Driven Forms – Simple & Declarative

What are Template-Driven Forms?

Template-driven forms rely on directives in the template (HTML) to create and manage the underlying form model. As an Engineering Manager, you might choose this approach for simple forms, such as a basic 'Contact Us' page or a quick search filter. It is heavily inspired by AngularJS (1.x) and uses two-way data binding to keep the component's variables in sync with the input fields.

  1. The Role of ngModel

The ngModel directive is the star of template-driven forms. It creates a FormControl instance from a domain model and binds it to a form control element. This allows for 'Banana-in-a-box' syntax [(ngModel)] which ensures that if the user types in the box, the variable updates, and vice versa.

HTMLRead-only
1

<input type="text" name="projectName" [(ngModel)]="project.name" required>
<p>Creating project: {{ project.name }}</p>

  1. Using the NgForm Directive

Angular automatically attaches the NgForm directive to any <form> tag. You can use a Template Reference Variable (like #projectForm) to export a reference to the NgForm instance, allowing you to check the overall validity of the form or reset it.

HTMLRead-only
1
<form #projectForm="ngForm" (ngSubmit)="onSubmit(projectForm)">
  <label>Project Title</label>
  <input type="text" name="title" ngModel required #title="ngModel">

  <div *ngIf="title.invalid && title.touched">
    Title is required!
  </div>

  <button type="submit" [disabled]="projectForm.invalid">Save</button>
</form>

  1. Validation in the Template

In this approach, validation is added using standard HTML attributes like required, minlength, and pattern. Angular recognizes these attributes and uses them to set the validation status of the associated FormControl.

Comparison: Template-Driven vs. Reactive

FeatureTemplate-DrivenReactive
Logic LocationTemplate (HTML)Component (TypeScript)
Data BindingTwo-way (ngModel)One-way (Observable)
TestingRequires DOM (Integration)Logic only (Unit)
ComplexityBest for simple formsBest for complex forms
Model ManagementImplicit / AsynchronousExplicit / Synchronous

Test Your Knowledge

Q1
of 3

Which directive is used for two-way data binding in template-driven forms?

A
formControl
B
ngBind
C
ngModel
D
formGroup
Q2
of 3

In a template-driven form, where is the validation logic primarily defined?

A
In the component's constructor
B
Directly in the HTML template using attributes
C
In a separate JSON configuration file
D
On the backend server
Q3
of 3

What is the mandatory attribute when using ngModel inside a form tag?

A
id
B
class
C
name
D
value

Frequently Asked Questions

Which module do I need to import?

You must import the 'FormsModule' from '@angular/forms' in your NgModule or Standalone Component to enable ngModel and NgForm.

Why do I need the 'name' attribute?

When using [(ngModel)] inside a <form> tag, the 'name' attribute is mandatory. Angular uses this name to register the control with the parent NgForm instance.

Can I use custom validators here?

Yes, but it's more complex than in Reactive forms. You have to create a custom Directive that implements the 'Validator' interface and provide it using 'NG_VALIDATORS'.

Previous

angular forms

Next

angular reactive forms

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.