In Angular, there are several types of control bindings that can be used in reactive forms. These bindings allow you to connect form controls to data sources and apply validation rules.
In this article, we'll look at different bindings available in Angular.
FormGroup
This binding connects a group of form controls to a data model. It creates a nested form structure, where the form controls are grouped together based on their functional relationship.
FormControl
This binding connects a single form control to a data model. It is used to create a single control instance for a form field.
FormArray
This binding connects an array of form controls to a data model. It creates dynamic forms where a user can add or remove form fields.
formControlName
This binding connects a single form control to a data model. It is used in template-driven forms to bind a form control to a data source.
formGroupName
This binding is used to group form controls under a common data model. It is used in template-driven forms to group related form controls.
Prerequisites
Before we begin, you should have a basic understanding of Angular and Reactive Forms. If you're unfamiliar with these topics, you can check out the official Angular documentation at the portal.
I have used the below tools to prepare this tutorial.
- Angular CLI - 15.1.6
- Node: 18.14.0
- NPM – 9.3.1
- Visual Studio Code
Without any delay, let us jump into the coding part.
The source code can be downloaded from GitHub
Create a Strongly Typed Reactive Form
Let's start by creating a new Angular application using the Angular CLI. Open a terminal window in Visual Studio Code and enter the following command:
ng new ReactiveControlBindings
Next, we will create a new component to host our typed reactive form. In the terminal window, enter the following command:
Cd reactivecontrolbindings
Ng generate component contactform
This will create a new " contact-form " component in the src/app directory. Open the component file at src/app/typed-form/contact-form.component.ts and add the following code:
import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-contact-form',
templateUrl: './contact-form.component.html',
styleUrls: ['./contact-form.component.scss']
})
export class ContactFormComponent implements OnInit {
fm = this.fb.group < FormModel > ({
title: this.fb.control('Personal Details'),
name: this.fb.group({
firstName: this.fb.control("PRTeck"),
lastName: this.fb.control("Talks")
}),
hobby: this.fb.array([
this.fb.control('Swimming'),
this.fb.control('Shuttle')
])
});
ngOnInit(): void {}
constructor(private fb: FormBuilder) {}
}
//Model
interface FormModel {
title: FormControl < string | null > ;
name: FormGroup < {
firstName: FormControl < string | null > ;
lastName: FormControl < string | null > ;
} > ;
hobby: FormArray < FormControl < string | null >> ;
}

Join the conversation! Your thoughts help the community grow.