Angular Forms - (Model Driven Forms/Reactive Forms )

In this article, we are going to talk about Angular Model-Driven forms/Reactive forms. I have already discussed template driven forms in my previous article:

In this article, we will learn how to use model-driven forms. As the name suggests, in a model-driven approach, the model which is created in the .ts file is responsible for handling all the user interactions/validations. For this, first, we need to create the Model using Angular’s inbuilt classes like formGroup and formControl and then, we need to bind that model to the HTML form.

For simplicity, I am using the same code which I have used in Template-Driven approach.

To use the Model-Driven approach, we need to import ReactiveFormsModule in our app.module.ts file.

app.module.ts file

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

Now, let’s open app.component.ts file and paste the below code.

  1. import { Component } from '@angular/core'  
  2. import { FormGroup, FormControl } from '@angular/forms'  
  4. @Component({  
  5.     selector: 'my-app',  
  6.     templateUrl: './app/app.component.html',  
  7. })  
  9. export class AppComponent {  
  10.     //userForm => model which is responsible for handling the user interactions  
  11.     userForm = new FormGroup({  
  12.         firstName: new FormControl(),  
  13.         email: new FormControl(),  
  14.         address: new FormGroup({  
  15.             country: new FormControl(),  
  16.             city: new FormControl(),  
  17.             postalCode: new FormControl()  
  18.         })  
  19.     });  
  21.     submitForm() {  
  22.         console.log(this.userForm.value);  
  23.     }  
  24. }  

To create a model, we need to use two important classes, i.e., FormGroup and FormControl.

I have created the userForm Model using the FormGroup class and added different controls like firstName, email, etc. using FormControl Class.

Let's open app.component.html and paste the following code to render the simple form.

HTML code

  1. <form class="form-horizontal" [formGroup]="userForm" (ngSubmit)="submitForm()" novalidate>  
  3.           <div class="form-group">  
  4.               <label class="control-label col-sm-2" for="name">First Name:</label>  
  5.               <div class="col-sm-10">  
  6.                   <input type="text" class="form-control" required placeholder="Enter first name" formControlName="firstName">  
  7.               </div>  
  8.           </div>  
  10.           <div class="form-group">  
  11.               <label class="control-label col-sm-2" for="email">Email:</label>  
  12.               <div class="col-sm-10">  
  13.                   <input type="text" class="form-control" placeholder="Enter email" formControlName="email">  
  14.               </div>  
  15.           </div>  
  16.           <div formGroupName="address">  
  17.               <div class="form-group">  
  18.                   <label class="control-label col-sm-2" for="country">Country:</label>  
  19.                   <div class="col-sm-10">  
  20.                       <input type="text" class="form-control" placeholder="Enter country" formControlName="country">  
  21.                   </div>  
  22.               </div>  
  24.               <div class="form-group">  
  25.                   <label class="control-label col-sm-2" for="city">City:</label>  
  26.                   <div class="col-sm-10">  
  27.                       <input type="text" class="form-control" placeholder="Enter city" formControlName="city">  
  28.                   </div>  
  29.               </div>  
  31.               <div class="form-group">  
  32.                   <label class="control-label col-sm-2" for="postalCode">Postal Code:</label>  
  33.                   <div class="col-sm-10">  
  34.                       <input type="text" class="form-control" placeholder="Enter postal code" formControlName="postalCode">  
  36.                   </div>  
  37.               </div>  
  38.           </div>  
  39.           <div class="form-group">  
  40.               <div class="col-sm-offset-2 col-sm-10">  
  41.                   <button type="submit" class="btn btn-primary">Submit</button>  
  42.               </div>  
  43.           </div>  
  44.       </form>  

HTML output screenshot
Angular Forms

In the HTML code, as you can see, I have used form tag at the top to declare the form.

<form class="form-horizontal" [formGroup]="userForm" (ngSubmit)="submitForm()" novalidate>

I have bound the model userForm (which is present in .ts file ) to the HTML form using the [formGroup] directive.

Whenever a user clicks on the submit button of this form, the ngSubmit event gets fired. The submitForm() function is present in .ts file.

And in the same way, we need to bind each and every form control with model properties using the formControlName inbuilt directive (see below code).

  1. < input type="text" class="form-control" required placeholder="Enter first name" formControlName="firstName">  

Now, refresh the browser and fill all the details.

Angular Forms
Click "Submit" and check the console log as shown below.

Angular Forms

Once we click the "Submit" button, it calls that submitForm function and console log as above.

In our next article, we will learn how to validate the data using Model-Driven forms.

You can download the complete code from my GitHub repository via this link.