Template-Driven Form

Template-driven forms are built around HTML-driven components. They rely heavily on directives in the template to handle form validation and data submission. As the name suggests, in Template-Driven Form, the form logic is mainly in the template file. It uses Angular’s “ngModel” two-way data-binding directive to create and manage the underlying form instance.

Syntax:

<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">
<input name="name" ngModel required>
<button type="submit">Submit</button>
</form>

Features of Template-Driven Forms

  • Easy to get started with, suitable for simple forms.
  • Minimal TypeScript code required.
  • Automatically tracks form and input state.
  • Two-way data binding with ngModel.
  • Built-in validation with HTML attributes.

Example:

Create a new project with the help of Angalur cli.

ng new forms
HTML
<!-- app.component.html -->

<h3>Complex Template Driven Form</h3>
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm.value)">
  <div>
    <label for="name">Name: </label>
    <input
      type="text"
      id="name"
      name="name"
      [(ngModel)]="credentials.name"
      required
    />
  </div>
  <div>
    <label for="email">Email: </label>
    <input
      type="email"
      id="email"
      name="email"
      [(ngModel)]="credentials.email"
      required
      email
    />
  </div>
  <div>
    <label for="password">Password: </label>
    <input
      type="password"
      id="password"
      name="password"
      [(ngModel)]="credentials.password"
      required
    />
  </div>
  <button type="submit" [disabled]="userForm.invalid">Submit</button>
</form>
JavaScript
//app.component.ts

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

@Component({
  selector: 'app-tdf-complex',
  standalone: true,
  imports: [FormsModule],
  templateUrl: './tdf-complex.component.html',
  styleUrl: './tdf-complex.component.css',
})
export class TdfComplexComponent {
  credentials: any = {};

  onSubmit(formValue: any) {
    console.log('Form value: ', formValue);
  }
}

Output:

Template Driven Form vs Reactive Form in Angular.

Angular provides two main approaches for creating forms: Template-Driven Forms and Reactive Forms. In this article, we’ll see both methods, highlighting their features, and providing code examples. We’ll also compare them side by side to help you understand when to use each.

Similar Reads

Template-Driven Form

Template-driven forms are built around HTML-driven components. They rely heavily on directives in the template to handle form validation and data submission. As the name suggests, in Template-Driven Form, the form logic is mainly in the template file. It uses Angular’s “ngModel” two-way data-binding directive to create and manage the underlying form instance....

Reactive Forms

In Reactive Forms, the validation logics and control are kept separated from the view template. These are managed programmatically in the component class using FormBuilder, FormGroup, FormControl, and FormArray. The template is only responsible for the basic structure of the form. This helps in scalability and maintainability if the form logic becomes too complex. Reactive Forms gives more control to the developers with regards to form state, validation, and form value changes....

Difference between Template-Driven Form and Reactive Form

Template-Driven Form (TDF) Reactive Form Form model setup Implicit in nature, as it relies on directives in the template to create and manipulate the underlying object model. Explicit in nature, as it provides direct access to the underlying form’s object model. Logic handling Form logic is mostly handled in the template. Form logic is handled in the component. Data flow Two-way data binding with NgModel directive provides asynchronous data update between view and model. Data flow between view and model is synchronous in nature as each form element in the view is directly linked to the form model with the help of a FormControl instance. Form validation Validations are done with the help of directives. Validations are done with the help of functions. Unit testing Tight coupling between the template and the component provides limited testability of form logic. Separation of concern between the view and model provides better testability of form logic....

Conclusion

Angular provides two approaches for building forms. On one hand, we have Template-Driven Form with its simple and familier syntax and simple learning curve, making it the go to choice for simple forms and rapid prototyping. If your form does not have complex logic and validations, which could be managed from within the template along with validation attributes integrated with HTML, then you should definitely go with Template-Driven approach....