Overview Of Forms In Angular

 Introduction

Forms are one of the most important parts of any business or enterprise application. Forms are used in many ways. They can be used during login, used in registering any new details, and so many other countless requirements. Forms are very important in guiding the end user towards effectively and efficiently going through the application workflow.

As a developer what we have to take care of includes:

  • Data binding in the form.
  • Change tracking.
  • Validation
  • Visual feedback
  • Error messages.
  • Form submission.

For a better experience in applications having forms, we should take care of all the above points.

Prerequisites

  1. HTML, CSS, JavaScript
  2. Angular – Template, components, data binding, services, routing

Basic knowledge of Angular and understanding the above requirements is enough to start with Angular forms.

Forms In Angular 

Template

The Component template contains the HTML view to collect the data from the end user.

Class

The component class handles the data binding and sends the data through to the service.

To achieve this Angular provides us with two approaches,

  1. Template-Driven forms
  2. Reactive/model driven Forms.

Template driven forms are used when most of the code is written in the component HTML template.

Reactive forms are used when most of the code is written in the component class.

For working with Angular forms we have to enable the type of form used. Let us start with Template-driven forms.

Enable template driven forms

Angular doesn’t give you functionality to use ngModel and other form-related tasks directly. We need to export them according to our usage,

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import {FormsModule} from '@angular/forms';  
  4. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';  
  5. @NgModule({  
  6.   declarations: [  
  7.     AppComponent,  
  8.   ],  
  9.   imports: [  
  10.     BrowserModule,  
  11.     FormsModule  
  12.   ],  
  13.   bootstrap: [AppComponent]  
  14. })  
  15. export class AppModule { }  

We have enabled template driven forms by adding FormsModule to our application.

Some of the key points of the template-driven forms are:

  • The template-driven form is easy to use and it is similar to the Angular js forms.
  • It mostly relies on the two-way data binding. Angular takes care of this binding with a ngModel directive.
  • Most of the code is the part of HTML and some of the logic resides in the component code.
  • In this approach, Angular provides ngForm directives which automatically track the forms and form elements, state, and validity along with ngModel directive.
  • Drawback – end to end testing with the browser is possible. The form validation logic cannot be unit tested.
  • Very difficult if complex forms and validations are required in usage.
  • If we have a simple form creation with unit test handling with the browser then go with the template-driven approach.
  • For complex forms with complex validity and unit test requirement, go with reactive forms.

Let us start with the simple application.

  1. Go to your project folder.
  2. Install latest angular cli.

    Run command : npm install -g @angular/@cli@latest
  1. Create new project > ng new TDFApplicatioin (template driven form application)
  2. > cd TDFApplication.

open app-component.ts and add the below contents,

  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-root',  
  5.   templateUrl: './app.component.html',  
  6.   styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.   groups=['A+''A-''B+''B-''O+''O-'];  
  10. }  

Open app-component.html and add the below contents,

  1. <h3>  
  2.   Student Registration!  
  3. </h3>  
  4. <p>Enter the below fields for student details.</p>  
  5.   
  6. <div>  
  7.   <form>  
  8.     <div>  
  9.       <label>Enter name : </label>  
  10.       <input type="text">  
  11.     </div>  
  12.     <div>  
  13.       <label>Enter Email : </label>  
  14.       <input type="email">  
  15.     </div>  
  16.     <div>  
  17.       <label>Enter Phone no : </label>  
  18.       <input type="tel">  
  19.     </div>  
  20.     <div>  
  21.       <div>  
  22.         select blood group:  
  23.       </div>  
  24.       <div>  
  25.         <select>  
  26.           <option selected>A+</option>  
  27.           <option *ngFor="let group of groups">{{group}}</option>  
  28.         </select>  
  29.       </div>  
  30.     </div>  
  31.     <div>  
  32.       <div>Select course:</div>  
  33.       <div>  
  34.         <input type="radio" name="course" value="BCA">BCA  
  35.       </div>  
  36.       <div>  
  37.         <input type="radio" name="course" value="B.Tech">B.Tech  
  38.       </div>  
  39.       <div>  
  40.         <input type="radio" name="course" value="BE">BE  
  41.       </div>  
  42.       <div>  
  43.         <input type="radio" name="course" value="B.sc">B.sc  
  44.       </div>  
  45.     </div>  
  46.     <br>  
  47. <div>  
  48.       <input type="checkbox"> <label>Send me updates.</label>  
  49.     </div>  
  50.     <div>  
  51.       <button type="submit">Submit</button>  
  52.     </div>  
  53.   </form>  
  54. </div>  

Now, we are done with creating forms and the next step is to capture the data input by the user to send it to the server.

Forms In Angular 

In our next article, we will see how the data will be bind in template driven form approach.


Similar Articles