Template Driven Forms In Angular

In our previous article, we learned how to create services and use them in multiple components. Now, we are going to talk about forms. Forms are really important to get the data from the users.

Let's say a user wants to sign in to our website. He/she must need to enter the username and password and then, we need to validate those credentials before the user enters the website. And we want to validate and show the errors if there is any wrong information while the user logs in. So, we must be able to access the data that user can enter in an input form.
 
In this article, we are going to see how we can fetch the data of the form and how we can validate the data and how we can handle different kinds of errors on that form.
 
Let's open app.component.ts and app.component.html which are both empty.
 
Now, I have prewritten CSS which you can write as it is; otherwise, you can write on your own also. 
 
Code
  1. *{  
  2.     box - sizing: border - box;  
  3. }.container {  
  4.     display: block;  
  5.     background: #f4f4f4;  
  6.     max - width: 650 px;  
  7.     margin - top: 20 px;  
  8.     margin - left: auto;  
  9.     margin - right: auto;  
  10.     padding: 10 px 40 px;  
  11.     text - align: center;  
  12.     position: relative;  
  13.     font - family: sans - serif;  
  14.     font - size: 15 px;  
  15. }  
  16. p {  
  17.     font - size: 40 px;  
  18.     font - weight: 200;  
  19.     line - height: 100 % ;  
  20.     font - weight: 700;  
  21.     color: #555459;  
  22. margin-top: 20px;  
  23. margin-left: 0px;  
  24. margin-bottom: 20px;  
  25. }  
  26.   
  27. input, select{  
  28. width:100%;  
  29. display: inline-block;  
  30. padding:15px;  
  31. font-weight: 100;  
  32. font-size: 20px;  
  33. margin:5px 0;  
  34. color:# 3 f3f3f;  
  35. }  
I will go back to app.component.html and design the form with the below HTML code.
 
Code
  1. <form class="container"> <input type="text" name="firstname" placeholder="FirstName"><br> <input type="text" name="lastname" placeholder="LastName"> <br> <select name="programming">  
  2.    <option value="C#">C#</option>  
  3.    <option value="VB.net">VB.net#</option>  
  4.    <option value="Anugular">Angular#</option>  
  5. </select> <br><br><br> <input type="submit" value="submit"> </form>  
Output



Now, we are done with our UI and our CSS is applied to these text boxes and dropdown list.
 
In order to fetch the data that is going to be written within input textboxes and an option from this dropdown list, we just need to get the data within our export class appComponent in app.component.ts. In Angular, there are two ways to fetch the data from the form as below.
  • Template driven form
  • Model-driven form 
In this article, let's see how we can fetch the data from a form using Template-driven approach.

So, let's go back to HTML, template driven form data means that most of the work we will be doing within HTML template to fetch the data of the form. And Model-driven form means that most of the work we will be doing within the app.component.ts to fetch the data of the form.
 
In Angular 4, we need to define a form to fetch the data of all the text boxes. It automatically detucts the form element and after that with in the form element it detucts that which of the elements contains directive and that directive is called ng Model.
 
Any input element whether its  a select or input elements, its going to check where are the ng Model written. ng Model is written just within the input text as shown below,
  1. <input type="text" name="firstname" placeholder="FirstName" ng-model><br>   
Copy and paste to other controls for select control, as shown below,
  1. <input type="text" name="firstname" placeholder="FirstName" ng-model><br> <input type="text" name="lastname" placeholder="LastName" ng-model> <br> <select name="programming" ng-model>  
  2. <option value="C#">C#</option>  
  3. <option value="VB.net">VB.net#</option>  
  4. <option value="Anugular">Angular#</option>  
  5. </select>  
In Angular 4 you must know what form should be used to send the different elements.

Let us write below code first as below,
  1. (ngSubmit)="onSubmit()"  
Function(onSubmit) will be inside the app.component.ts, whenever the form is submitting then which function will be called and that function will receive all the values that will return within the input elements. 

And along with ngSubmit directive we need to define another directive and that is called ng form, this is built in Angular 4.
 
Code
  1. <form class="container"#userForm= "ngForm"(ngsubmit)="onSubmit(userForm.value)"
We wrote userForm.value, this will send out the objects of all the values that are going to be written in all the input elements and select element as well.Every element has ngModel directive.

Now write onSubmit function within the app.component.ts as below,
  1. onsubmit=function(user)  
  2. {  
  3.    console.log(user);  
  4. }  
We wrote console.log to check if we are getting values or not and also to make sure that in your app.module.ts file you have imported formsmodule library. Because without it we can't work with form validations.
 
Now refresh the browser and type in FirstName, LastName and select any language as shown below,
  
 
Click submit and check the console log as shown below,

 
Once we click on submit button, it has called that onsubmit function and console log is as above.
 
So that was it for the Template Driven Forms. In our next article, we shall learn about Model driven forms.
 
Sharing is caring.