Friday, 13 December 2024

Simple Angular Template Driven Form

Angular provides two types of forms in Angular namely Template Driven Form and Reactive Form. On this post, we will see a simple example of a Template Driven Form. Angular version used here is 19.

First, add FormsModule in app.module.ts:
import { FormsModule } from '@angular/forms';
. . .

@NgModule({
  declarations: [. . .],
  imports: [
    . . .
    FormsModule,
    . . .
  ],
  providers: [. . .  ]
})

Then the HTML elements:
<form #studentForm="ngForm" (ngSubmit)="submitStudentForm()">
  <div>
    <label for="firstName">Student Name:</label>
    <input type="text" id="firstName" [(ngModel)]="model.firstName" name="firstName" placeholder="First Name" />
    <input type="text" id="lastName" [(ngModel)]="model.lastName" name="lastName" placeholder="Last Name" />
  </div>
  <div>
    <label for="email">Email:</label>
    <input type="text" id="email" [(ngModel)]="model.email" name="email" />
  </div>
  <button type="submit">Update</button>
</form>
Note that we need to add a template reference variable, '#theFormName' and set its value to ngForm, in our example is <form #studentForm="ngForm">. In the example we also use [(ngModel)] on input fields for two ways data binding. name attribute for each input is also required by ngForm, otherwise it won't work.

Finally on the .ts file:
export class StudentComponent {

  model: Student = {
    firstName: 'John',
    lastName: 'Doe',
    email: 'john.doe@example.com'
  };

  submitStudentForm(): void {
	. . .
  }
}

3 comments:

dean said...

The article provides a clear and practical introduction to Angular template-driven forms using Angular 19. The example explains how to import FormsModule, create a form using the ngForm directive, use [(ngModel)] for two-way data binding, and define the required name attributes for form controls. These details make the example useful for developers learning how Angular manages form state and user input. For learners who want to explore Angular forms and related application concepts in greater depth, Angular Training can provide a structured learning path.

dean said...

The example also demonstrates how the form connects the HTML template with the TypeScript component through the model object and the submitStudentForm() method. Understanding this relationship is important when developing maintainable Angular applications, particularly when moving from basic templates toward more structured application logic. Since modern Angular development relies heavily on TypeScript, TypeScript Training can also help developers build a stronger foundation for working with Angular components and application models.

dean said...

Practical examples such as this are especially useful for students because form handling is a common requirement in real-world applications. Implementing student registration, profile forms, data-entry screens, and validation workflows can provide valuable hands-on experience, and Angular Projects can provide opportunities to apply these concepts within larger project implementations.