Introduction

In this article, I am going to tell you how to implement Template Driven Form in the Angular 11 project. In this article, I am covering how to create projects, implement template-driven forms, get forms data, set data in form and validation.
In this article, I’m using bootstrap for designing forms so if you do not know how to add bootstrap in Angular 11 then please refer to the below article first.
In This Article,:

Create New Project

On my pc, there is already Angular’s latest version installed. If you do not have angular in your system you can install it in a few steps. You can refer to angular’s official website to learn how to install angular.
Run the following command to create a new Angular project. Here ng means Angular, new determine that create a new project and FormsInAngular is the project name.
  1. ng new FormsInAngular
After running this command you will ask some questions about add routing and which style sheet you want to use in your project. You can choose as per your requirement.
Template Driven Form In Angular 11

Create Simple Form Design

Create a form as per your requirement here I create a form that has textbox, dropdown, and checkbox.
  1. <div class="row mt-1">
  2. <form>
  3. <div class="form-group col-6 m-2 p-2">
  4. <label for="inputAddress2">Name</label>
  5. <input type="text" class="form-control" name="name" id="name" placeholder="Name" >
  6. </div>
  7. <div class="form-group col-6 m-2 p-2">
  8. <label for="inputEmail4">Email</label>
  9. <input type="email" class="form-control" id="email" name="email" placeholder="Email" >
  10. </div>
  11. <div class="form-group col-6 m-2 p-2">
  12. <label for="inputPassword4">Password</label>
  13. <input type="password" class="form-control" id="password" name="password" placeholder="Password" >
  14. </div>
  15. <div class="form-group col-6 m-2 p-2">
  16. <label for="inputAddress">Address</label>
  17. <input type="text" class="form-control" name="address" id="address" placeholder="1234 Main St" >
  18. </div>
  19. <div class="form-group col-6 m-2 p-2">
  20. <label for="inputCity">City</label>
  21. <input type="text" class="form-control" id="city" name="city" >
  22. </div>
  23. <div class="form-group col-4 m-2 p-2">
  24. <label for="state">State</label>
  25. <select id="state" name="state" class="form-control" >
  26. <option selected>Choose...</option>
  27. <option>Gujarat</option>
  28. <option>UP</option>
  29. <option>Delhi</option>
  30. </select>
  31. </div>
  32. <div class="form-group col-3 m-2 p-2">
  33. <label for="inputZip">Zip</label>
  34. <input type="text" class="form-control" id="zip" name="zip" >
  35. </div>
  36. <div class="form-group m-2 p-2">
  37. <div class="form-check">
  38. <input class="form-check-input" type="checkbox" id="isMarried" name="isMarried" >
  39. <label class="form-check-label" for="isMarried">
  40. Married
  41. </label>
  42. </div>
  43. </div>
  44. <button type="submit" class="btn btn-primary m-2 p-2">Submit Form</button>
  45. </form>
  46. </div>

Make Form Template Driven Form

Step 1
Add FormsModule in your Module import array. For using forms in Angular we need to import this.
  1. import { FormsModule } from '@angular/forms'; // Import this module for use forms
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { NgModule } from '@angular/core';
  4. import { AppComponent } from './app.component';
  5. @NgModule({
  6. declarations: [
  7. AppComponent
  8. ],
  9. imports: [
  10. BrowserModule,
  11. FormsModule // Add this module for use forms
  12. ],
  13. providers: [],
  14. bootstrap: [AppComponent]
  15. })
  16. export class AppModule { }
Step 2
Assign your form as ngForm as below give your form name using # and assign ngForm to it. Now your form is treated as template-driven form.
  1. <form #DemoForm="ngForm"></form>

Get form Data with name attribute

Step 1
Add ngModel attribute in an element.
Step 2
As you see in the above form design that we are given name attribute to every element because if we are using ngModel and not given name attribute that it gives an error like below. So if you do not need to give a name to your element give it.
Template Driven Form In Angular 11
Step 3
Add submit event in form and call that function which we will create in .ts file and pass your form name without # in the parameter.
  1. <form #DemoForm="ngForm" (ngSubmit)="onSubmit(DemoForm)">
Step 4
Create a function in .ts file to show data that takes one parameter form. You can get the value of form in an object with property same as your name given in name attribute. For value use YourFormParament.value .
  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.scss']
  6. })
  7. export class AppComponent {
  8. title = 'FormsInAngular';
  9. onSubmit(DemoForm){
  10. console.log(DemoForm.value);
  11. }
  12. }

Output
Template Driven Form In Angular 11

Get Data using Local Variable

We can also use local variables or objects for getting data from the form. Here we define some variables in .ts file and use them in form. Here are ts file changes.
  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.scss']
  6. })
  7. export class AppComponent {
  8. title = 'FormsInAngular';
  9. name: string = "";
  10. city: string = "";
  11. state: string = "";
  12. address: string = "";
  13. zip: string = "";
  14. email: string = "";
  15. password: string = "";
  16. isMarried: boolean;
  17. onSubmit(DemoForm) {
  18. //local variable
  19. console.log("Name :- " + this.name);
  20. console.log("City :- " + this.city);
  21. console.log("State :- " + this.state);
  22. console.log("Zip :- " + this.zip);
  23. console.log("Email :- " + this.email);
  24. console.log("Password :- " + this.password);
  25. console.log("IsMarried :- " + this.isMarried);
  26. }
  27. }
On the design side change ngModel attribute as below. Assign the variable which you created. If you create an object like a person and in-person you have a name, email, etc. then assign it as a person.name, person.email, etc.
  1. <div class="row mt-1">
  2. <form #DemoForm="ngForm" (ngSubmit)="onSubmit(DemoForm)">
  3. <div class="form-group col-6 m-2 p-2">
  4. <label for="inputAddress2">Name</label>
  5. <input type="text" class="form-control" name="name" id="name" placeholder="Name" [ngModel]="name">
  6. </div>
  7. <div class="form-group col-6 m-2 p-2">
  8. <label for="inputEmail4">Email</label>
  9. <input type="email" class="form-control" id="email" name="email" placeholder="Email" [ngModel]="email">
  10. </div>
  11. <div class="form-group col-6 m-2 p-2">
  12. <label for="inputPassword4">Password</label>
  13. <input type="password" class="form-control" id="password" name="password" placeholder="Password" [ngModel]="password">
  14. </div>
  15. <div class="form-group col-6 m-2 p-2">
  16. <label for="inputAddress">Address</label>
  17. <input type="text" class="form-control" name="address" id="address" placeholder="1234 Main St" [ngModel]="address">
  18. </div>
  19. <div class="form-group col-6 m-2 p-2">
  20. <label for="inputCity">City</label>
  21. <input type="text" class="form-control" id="city" name="city" [ngModel]="city">
  22. </div>
  23. <div class="form-group col-4 m-2 p-2">
  24. <label for="state">State</label>
  25. <select id="state" name="state" class="form-control" [ngModel]="state">
  26. <option selected>Choose...</option>
  27. <option>Gujarat</option>
  28. <option>UP</option>
  29. <option>Delhi</option>
  30. </select>
  31. </div>
  32. <div class="form-group col-3 m-2 p-2">
  33. <label for="inputZip">Zip</label>
  34. <input type="text" class="form-control" id="zip" name="zip" [ngModel]="zip">
  35. </div>
  36. <div class="form-group m-2 p-2">
  37. <div class="form-check">
  38. <input class="form-check-input" type="checkbox" id="isMarried" name="isMarried" [ngModel]="isMarried">
  39. <label class="form-check-label" for="isMarried">
  40. Married
  41. </label>
  42. </div>
  43. </div>
  44. <button type="submit" class="btn btn-primary m-2 p-2">Submit Form</button>
  45. </form>
  46. </div>
Output
Template Driven Form In Angular 11

Set Data in Form

Giving value to elements in Template-driven forms is very easy. You need to assign value to a variable or object which you use. As you can see below here I only assign data to variables and it’s reflected in form. ngModel has two-way binding so we can get data using variable and also bind data using that variable.
  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.scss']
  6. })
  7. export class AppComponent {
  8. title = 'FormsInAngular';
  9. name: string = "Virat Kohli";
  10. city: string = "Delhi";
  11. state: string = "Delhi";
  12. address: string = "123 Delhi";
  13. zip: string = "12345";
  14. email: string = "[email protected]";
  15. password: string = "12345";
  16. isMarried: boolean = true;
  17. onSubmit(DemoForm) {
  18. //forms value
  19. console.log(DemoForm.value);
  20. //local variable
  21. console.log("Name :- " + this.name);
  22. console.log("City :- " + this.city);
  23. console.log("State :- " + this.state);
  24. console.log("Zip :- " + this.zip);
  25. console.log("Email :- " + this.email);
  26. console.log("Password :- " + this.password);
  27. console.log("IsMarried :- " + this.isMarried);
  28. }
  29. }
Output
Template Driven Form In Angular 11

Validation in Form

If you see any type of form it has validation. In simple form, it also has at least required validation. In Template Driven form we can also use validation.
Here are changes which you need to do in your existing form.
Template Driven Form In Angular 11
Explanation
Here is full html source code of form,
  1. <div class="row mt-1">
  2. <form #DemoForm="ngForm" (ngSubmit)="DemoForm.form.valid&& onSubmit(DemoForm)">
  3. <div class="form-group col-6 m-2 p-2">
  4. <label for="inputAddress2">Name</label>
  5. <input type="text" class="form-control" name="name" id="name" placeholder="Name" required [(ngModel)]="name" #nameVar="ngModel">
  6. <div class="text-danger" *ngIf="DemoForm.submitted && nameVar.errors" >
  7. <p *ngIf="nameVar.errors.required"> Name Is Required.</p>
  8. </div>
  9. </div>
  10. <div class="form-group col-6 m-2 p-2">
  11. <label for="inputEmail4">Email</label>
  12. <input type="email" class="form-control" id="email" name="email" placeholder="Email" [(ngModel)]="email" #emailVar="ngModel" required>
  13. <div class="text-danger" *ngIf="DemoForm.submitted &&emailVar.errors" >
  14. <p *ngIf="emailVar.errors.required"></p>Email Is Required.
  15. </div>
  16. </div>
  17. <div class="form-group col-6 m-2 p-2">
  18. <label for="inputPassword4">Password</label>
  19. <input type="password" class="form-control" id="password" name="password" placeholder="Password" [(ngModel)]="password" #passwordVar="ngModel">
  20. </div>
  21. <div class="form-group col-6 m-2 p-2">
  22. <label for="inputAddress">Address</label>
  23. <input type="text" class="form-control" name="address" id="address" placeholder="1234 Main St" [(ngModel)]="address" #addressVar="ngModel">
  24. </div>
  25. <div class="form-group col-6 m-2 p-2">
  26. <label for="inputCity">City</label>
  27. <input type="text" class="form-control" id="city" name="city" [(ngModel)]="city" #cityVar="ngModel">
  28. </div>
  29. <div class="form-group col-4 m-2 p-2">
  30. <label for="state">State</label>
  31. <select id="state" name="state" class="form-control" [(ngModel)]="state" #stateVar="ngModel">
  32. <option selected>Choose...</option>
  33. <option>Gujarat</option>
  34. <option>UP</option>
  35. <option>Delhi</option>
  36. </select>
  37. </div>
  38. <div class="form-group col-3 m-2 p-2">
  39. <label for="inputZip">Zip</label>
  40. <input type="text" class="form-control" id="zip" name="zip" [(ngModel)]="zip" #zipVar="ngModel">
  41. </div>
  42. <div class="form-group m-2 p-2">
  43. <div class="form-check">
  44. <input class="form-check-input" type="checkbox" id="isMarried" name="isMarried" [(ngModel)]="isMarried" #isMarriedVar="ngModel">
  45. <label class="form-check-label" for="isMarried">
  46. Married
  47. </label>
  48. </div>
  49. </div>
  50. <button type="submit" class="btn btn-primary m-2 p-2">Submit Form</button>
  51. </form>
  52. </div>
Output
Template Driven Form In Angular 11

Conclusion

So here we created Template Driven form, set data in form, get data from a form and also use validation. If you find this article useful kindly like this article and share it with your friends. Thank You.