Skip to content
tezvyn:

Angular Template-Driven Forms: Logic in the HTML

Source: angular.devEasyHow cards are made

Angular Template-Driven Forms: Logic in the HTML

Angular Template-Driven Forms put form logic directly in your HTML template. Using two-way data binding with [(ngModel)], they're great for simple scenarios like login or contact forms.

Why it exists

To provide a simple, declarative way to build forms in Angular for straightforward use cases. It allows developers to define form behavior, data binding, and validation directly within the HTML template, minimizing component code for simple scenarios.

The mental model

Think of the HTML template as the "boss" of the form. You declare the form's shape, its connection to your data model, and its validation rules directly in the HTML using special Angular attributes (directives). The component class holds the data, but the template drives the interaction and structure.

How it works

First, you must import FormsModule into your Angular module. In your component's template, you use the [(ngModel)] syntax on an input element. This creates a two-way data binding that syncs the input's value with a property in your component. Angular automatically attaches CSS classes like ng-valid, ng-invalid, ng-dirty, and ng-touched to the form controls, allowing you to style them based on their state. The entire form is managed by an ngForm directive that Angular adds automatically to the <form> tag, tracking its overall value and validity. Form submission is handled by binding the (ngSubmit) event on the <form> element to a method in your component.

When to use it

Use it for small, simple forms with a fixed structure. It's an excellent choice for login forms, contact forms, or newsletter sign-ups where the fields and validation rules are static. The setup is quick and keeps the logic co-located with the view.

When not to use it

Avoid template-driven forms for complex, dynamic scenarios. If you need to programmatically add or remove form controls, implement complex cross-field validation, or manage a form whose structure changes based on user input, you should use Reactive Forms. The template-driven approach becomes difficult to manage and test at scale.

One canonical example

A simple user contact form. The component has a contact object with name and email properties. The template has an input like <input type="text" [(ngModel)]="contact.name" name="name" required>. The name attribute is crucial; it registers the control with the form. The required attribute adds a simple validation rule. The form's submit button can be disabled until the form is valid by checking the form's state, like [disabled]="!contactForm.form.valid".

Interview question

When is it most appropriate to use Angular Template-Driven Forms?

  • a.To facilitate easy unit testing of form logic independently from the template.
  • b.For forms that require extensive programmatic control and complex validation.
  • c.When building simple forms with a fixed structure, such as a login or contact form.Correct
  • d.When the form structure needs to change dynamically based on user input.
Why?

Template-Driven Forms are explicitly recommended for small, simple forms with a fixed structure like login or contact forms, as they keep logic co-located with the view. Complex or dynamic form scenarios, extensive programmatic control, and advanced testing are better handled by Reactive Forms.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles