Reactive Forms in Angular

Deepak Pandey
Oct 2 · 3 min read

Angular provides two types of forms:

Template-driven forms: Template directives are used to build an internal representation of the form. Here the source of truth lies in the template.

In template-driven forms, each form element is linked to a directive that manages the form model internally.

Reactive forms: provides a model-driven approach to handling form inputs whose values change over time. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.

With reactive forms, one has the power to build his representation of form in the component class.

In reactive forms, the source of truth lies in the component class, i.e. the form model (like FormGroup or FormControl instance). The update from the model to the view and from the view to the model is synchronous.

Classes which form the base of the Angular Forms:

  1. AbstractControl: Base class for FormControl, FormGroup, and FormArray. It provides the shared properties and behavior that all controls and groups have.
  2. FormControl: Tracks the value and validation status of an individual form control. It maps to the single form element in the template.
  3. FormGroup: Tracks the value and validity state of a group of FormControl instances. It aggregates the values of each child FormControl into one object, with each control name as the key.
  4. FormArray: Tracks the value and validity state of an array of FormControl, FormGroup or FormArray instances. aggregates the values of each child FormControl into an array.
  5. FormBuilder: A utility class used to create an AbstractControl from a user-specified configuration. It is used to create the instances of a FromControl, FormGroup, or FormArray.

Let us now get started with reactive forms in Angular.

Importing the ReactiveFormsModule

To work with ReactiveForms we will need to import the ReactiveFormsModule in our module.

ReactiveFormsModule added to imports array of AppModule

Template containing the form:

app.component.html

formGroup: This directive allows us to give a name to our form. The form will be mapped to an instance of the FormGroup in the component class.

ngSubmit: The event triggered when the form is submitted. Here in our code when the form is submitted onSubmit method of the component is called.

formControlName: The directive takes a value which is the name for the FormControl in the component class.

The Component Class:

Here in the component, we are using the FormBuilder service to create the FromGroup instance. The form name registerForm matches the value passed to formGroup property in the form template.

Each formControlName property value in the form template is mapped with the FormControl instance in the registerForm group. The FormControl instance can also take an array of Validators, that can be used to add the validation to the form.

This is all the required basics to implement the Reactive Forms..

JavaScript in Plain English

Learn the web's most important programming language.

Deepak Pandey

Written by

Angular | Angular JS | JavaScript | Python | Java

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade