AbstractControl vs FormControl

Sophie Andriamahenintsoa
2 min readDec 20, 2023

In Angular, reactive forms are a way to manage and manipulate forms in a reactive and predictable manner. FormControl and AbstractControl are both important concepts within the Angular reactive forms API.

1. AbstractControl:

  • AbstractControl is a base class for form controls in Angular.
  • It provides the basic functionality for form controls, such as tracking their value, validity, and user interactions.
  • Both FormControl and FormGroup extend AbstractControl, making it a fundamental building block for form controls.

2. FormControl:

  • FormControl is a class that extends AbstractControl and represents a single input field in a form.
  • It is used to track the value and validation status of an individual form control (e.g., an input field).
  • You create a FormControl instance for each input field in your form.

Differences:

Hierarchy:

  • FormControl is a specific implementation of AbstractControl.
  • AbstractControl serves as a more general class that is extended by both FormControl and FormGroup.

Use cases:

  • AbstractControl is more of a general concept and is not directly used to represent a specific input field. It is more abstract and can be used when you need to work with a group of controls, which is common in the case of FormGroup.
  • FormControl is specifically designed to represent the state and behavior of an individual form control (e.g., an input field).

Creation:

  • You create a FormControl for each individual input field in your form.
  • AbstractControl is not typically instantiated directly for form controls; instead, you use FormControl for individual controls and FormGroup for groups of controls.
import { Component } from '@angular/core';
import { FormControl, FormGroup, AbstractControl } from '@angular/forms';

@Component({
selector: 'app-my-form',
template: `
<form [formGroup]="form">
<input type="text" formControlName="userName">
</form>
`
,
})
export class MyFormComponent {
// Using FormControl for an individual input field
userNameControl: FormControl = new FormControl();

// Using AbstractControl for a group of controls
form: FormGroup = new FormGroup({
userName: new FormControl(),
password: new FormControl(),
});

// AbstractControl can represent a group or an individual control
abstractControlExample: AbstractControl = this.form;
}

In summary, FormControl is a specialized implementation of AbstractControl tailored for individual form controls, while AbstractControl provides a more general representation that can be used for both individual controls and groups of controls (e.g., FormGroup).

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response