Learn About Reactive Forms In Angular

In this article, you will learn about reactive forms in Angular.

Introduction

 
In Angular, the reactive forms are use to create the forms in which the code and logic resides in the component class. Unlike Template-driven forms where more focus is on HTML templates, we focus on component for logics. We can perform Two way binding in Template driven forms but there is no two way binding in Reactive forms. Angular provides the methods to update the values from the component class.
 
Reactive forms are used on complex cases, like dynamic forms element, dynamic validations etc.
 
Prerequisites
  • HTML, CSS, and JS
  • Basics of angular

Implementation

 
Let us create a simple application that utilizes Reactive forms.
 
Create a TestApp project using Angular CLI.
 
Create a test component which will contain the forms and related field:
 
Open test.component.html and add the below contents:
  1. <div class="container-fluid">  
  2.     <h3>Registration Form</h3>  
  3.     <form>  
  4.         <div class="form-group">  
  5.             <label>UserName</label>  
  6.             <input type="text" class="form-control">  
  7.         </div>  
  8.         <div class="form-group">  
  9.             <label>Password</label>  
  10.             <input type="Password" class="form-control">  
  11.         </div>  
  12.         <div class="form-group">  
  13.             <label>Email Address</label>  
  14.             <input type="email" class="form-control">  
  15.         </div>  
  16.         <div class="form-group">  
  17.             <label>Phone</label>  
  18.             <input type="Phone" class="form-control">  
  19.         </div>  
  20.         <button class="btn btn-primary" type="submit">Register</button>   
  21.     </form>  
  22. </div>  
Open app.module.ts and edit with the below contents,
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { ReactiveFormsModule } from '@angular/forms';  
  4. import { AppComponent } from './app.component';  
  5. import { TestComponent } from './test/test.component';  
  6.   
  7. @NgModule({  
  8.   declarations: [  
  9.     AppComponent,  
  10.     TestComponent  
  11.   ],  
  12.   imports: [  
  13.     BrowserModule,  
  14.     ReactiveFormsModule  
  15.   ],  
  16.   providers: [],  
  17.   bootstrap: [AppComponent]  
  18. })  
  19. export class AppModule { }  
Basically we have imported ReactiveFormsModule and added it to imports array.
 
ReactiveFormsModule comes with a lot of classes and directives that are necessary to create the Reactive Forms.
 
Two classes are used to build up the Reactive forms,
  1. FormGroup
  2. FormControl
Form is represented by a model in the component class.
 
In our form under test.component.html there are four fields UserName, Password, Email Address, Phone. These fields are the instance of FormControl. And overall all the FormControl are defined as the instance of FormGroup class.
 
So overall form is represented as FormGroup and each field is represented as FormControl in the component.
 
Now open test.component.ts and edit with below contents,
  1. import { Component, OnInit } from '@angular/core';  
  2. import { FormGroup, FormControl } from '@angular/forms';  
  3.   
  4. @Component({  
  5.   selector: 'app-test',  
  6.   templateUrl: './test.component.html',  
  7.   styleUrls: ['./test.component.css']  
  8. })  
  9. export class TestComponent implements OnInit {  
  10.   
  11.   registrationForm: any;  
  12.   constructor() { }  
  13.   
  14.     
  15.   ngOnInit() {  
  16.     this.registrationForm= new FormGroup({  
  17.      userName : new FormControl('Irshad'),  
  18.      password : new FormControl(''),  
  19.      email : new FormControl(''),  
  20.      phone : new FormControl('')  
  21.    });  
  22.   }  
  23. }  
Open app.component.html and edit with the below contents,
 
Basically we are trying to bind the FormGroup “registrationForm” to our html Form and the form control to the corresponding html element in form.
  1. <div class="container-fluid">  
  2.     <h3>Registration Form</h3>  
  3.     <form [formGroup]="registrationForm">  
  4.         <div class="form-group">  
  5.             <label>UserName</label>  
  6.             <input formControlName="userName" type="text" class="form-control">  
  7.         </div>  
  8.         <div class="form-group">  
  9.             <label>Password</label>  
  10.             <input formControlName="password" type="Password" class="form-control">  
  11.         </div>  
  12.         <div class="form-group">  
  13.             <label>Email Address</label>  
  14.             <input formControlName="email" type="email" class="form-control">  
  15.         </div>  
  16.         <div class="form-group">  
  17.             <label>Phone</label>  
  18.             <input formControlName="phone" type="Phone" class="form-control">  
  19.         </div>  
  20.         <button class="btn btn-primary" type="submit">Register</button>   
  21.     </form>  
  22.     {{registrationForm.value | json}}  
  23. </div>  
The entire form will be associated with the formGroup registrationForm and the corresponding formControls from the model is associated with the element in html.
 
So we are done with creating the reactive form.
 
Run the application,
 
Reactive Forms In Angular
 
You can see the fields are getting bound with the corresponding component’s declared registrationForm and all the fields are getting bound properly.
 
Also, we have to display the values via interpolation with JSON pipe notation.
 
Thank you.