Basic Angular Form Design Using Template Driven Method

It’s been a long time since I wrote articles. Well, in the future, I will share my knowledge with you. This time, I will be sharing articles on Angular since I have started to learn it from scratch. Let's begin.
When a user or a community needs to gather some information from the user/s, forms ware is considered the most effective way of achieving this. It provides an interface where a user can fill all the required details and validate the information before submitting the data. In Angular, we have two different ways of creating forms - Template Driven Forms and Reactive Forms.
In this article, we shall take a look at the process of creating Template-Driven forms and their implementation in Angular. So, let's start!

Create a new Angular Project

Open the terminal using either Command Prompt or Visual Studio Code terminal and follow the given steps for creating a new project. 
First, create a project named "validation" using the following command. 
ng new validation
Basic Form Design Using Template Driven Method
Now, after creating a new Angular project, we can move forward to a practical demo and create template-Driven forms for implementation step by step.
Execute this command for adding bootstrap to your project so that the forms can be responsive to devices we are using.
npm install bootstrap
Basic Form Design Using Template Driven Method

Implementation of Template-Driven Forms

After installing bootstrap, we can use the bootstrap classes. So, open styles.css and add the following code which is responsible for importing bootstrap CSS from installed node packages.
@import "node_modules/bootstrap/dist/css/bootstrap.min.css";
Coming to Angular 8, we don’t need to add the module which is essential for providing all the required components that are created in a template-driven method, such as adding Forms Module in the app.module.ts using Angular previous versions.
Next, we need to create a child component and name the component as "child". This is where we will be creating a form with that. For adding a new component, change the directory to the app folder in terminal and execute the command as follows.
ng g c child
Basic Form Design Using Template Driven Method
Then, let us create a simple form in which a user can enter their details. So, add the code inside ‘child.component.html'.
  1. <div class="row">  
  2.     <div class="col-md-8" style="margin-top: 30px;  
  4. border: 5px solid powderblue; padding: 50px;">  
  5.         <form class="form-horizontal" role="form">  
  6.             <fieldset>  
  7.                 <strong>Basic Design Template Driven Form </strong>  
  8.                 <div class="form-group">  
  9.                     <label class="col-sm-5 control-label" for="textinput">FirstName</label>  
  10.                     <div class="col-sm-8">  
  11.                         <input type="text" name="FirstName" placeholder="Enter FirstName" class="form-control">  
  12.                         </div>  
  13.                     </div>  
  14.                     <div class="form-group">  
  15.                         <label class="col-sm-5 control-label" for="textinput">LastName</label>  
  16.                         <div class="col-sm-8">  
  17.                             <input type="text" name="LastName" placeholder="Enter LastName" class="form-control">  
  18.                             </div>  
  19.                         </div>  
  20.                         <div class="form-group">  
  21.                             <label class="col-sm-5 control-label" for="textinput">Address</label>  
  22.                             <div class="col-sm-8">  
  23.                                 <input type="text" name="Address" placeholder="Enter Address" class="form-control">  
  24.                                 </div>  
  25.                             </div>  
  26.                             <div class="form-group">  
  27.                                 <label class="col-sm-5 control-label" for="textinput">City</label>  
  28.                                 <div class="col-sm-8">  
  29.                                     <input type="text" name="city" placeholder="Enter City Name" class="form-control">  
  30.                                     </div>  
  31.                                 </div>  
  32.                                 <div class="form-group">  
  33.                                     <label class="col-sm-5 control-label" for="textinput">State</label>  
  34.                                     <div class="col-sm-4">  
  35.                                         <input type="text" name="state" placeholder="State" class="form-control">  
  36.                                         </div>  
  37.                                         <label class="col-sm-5 control-label" for="textinput">Postcode</label>  
  38.                                         <div class="col-sm-4">  
  39.                                             <input type="text" name="postcode" placeholder="Enter Post Code" class="form-control">  
  40.                                             </div>  
  41.                                         </div>  
  42.                                         <div class="form-group">  
  43.                                             <label class="col-sm-5 control-label" for="textinput">Gender</label>  
  44.                                             <div class="col-sm-8">  
  45.                                                 <select class="form-control" name="Gender">  
  46.                                                     <option>---Select---</option>  
  47.                                                     <option *ngFor="let item of GenderData" [value]="item">  
  49. {{item}}  
  51. </option>  
  52.                                                 </select>  
  53.                                             </div>  
  54.                                         </div>  
  55.                                         <br>  
  56.                                             <div class="form-group">  
  57.                                                 <div class="col-sm-offset-5 col-sm-8">  
  58.                                                     <div class="button">  
  59.                                                         <button type="submit"> Save</button>  
  60.                                                         <button type="reset">Reset</button>  
  61.                                                     </div>  
  62.                                                 </div>  
  63.                                             </div>  
  64.                                         </fieldset>  
  65.                                     </form>  
  66.                                 </div>  
  67.                             </div>  
 Basic Form Design Using Template Driven Method
In the above child-component.html, the form is a simple form and before converting the above simple form into Template Driven Forms, we will be creating a Model as following.
  1. export interface address check {  
  2.     FirstName: string,  
  3.     LastName: string,  
  4.     address: string,  
  5.     city: string,  
  6.     state: string,  
  7.     postcode: number,  
  8.     Gender: any[],  
  9. }  
Now, let us modify the 'child.component.ts' file. Here, we first construct the data for the Gender drop-down and construct a model with a type of default values.
  1. GenderData: any[] = ['Male’, ‘Female'];  
  2. check = {  
  3.         FirstName: '',  
  4.         LastName: '',  
  5.         address: '',  
  6.         city: '',  
  7.         state: '',  
  8.         postcode: null,  
  9.         Gender: null,  
So, we have built a simple Template Driven Form. Let’s run the project and see the output. For that, switch to the terminal and execute this command to view the output in the browser.
ng serve --open
Basic Form Design Using Template Driven Method 
Basic Form Design Using Template Driven Method
I hope this article will be useful to you. Please put your feedback in the comments section which helps me to improve for the next article. If you have any query, feel free to ask.
Thank You!!!