Reactive Forms
Bind a form to a Reactive Forms in Angular.
Currently, the form input is possible. Now, you need to retrieve the form data to create a new task and add it to the existing list.
Angular Forms API
Section titled “Angular Forms API”Angular provides 2 different APIs for working with forms:
- Template-driven Forms (two-way binding with the
ngModeldirective) - Reactive Forms
In this course, you will use the Reactive Forms API to bind the form.
Reactive Forms
Section titled “Reactive Forms”The form structure, its behavior and related validation rules are defined in the component file.
These rules are then bound to native HTML input tags in the HMTL file using the formControlName directive. We will see how to do that in the next steps.
It enables connecting the template to the Form and reactively be aware of any change about its value, its status and the current state of the validation rules.
FormGroup and FormControl
Section titled “FormGroup and FormControl”FormGroup and FormControl are two classes exposed by the @angular/formspackage to define and structure your form:
FormControlis used to define the rules of a single input.FormGroupis used to wrap multipleFormControlto manipulate a set of inputs as a single entity (it acts as a wrapper for a group of controls).
FormGroup
Section titled “FormGroup”We’ll firstly define the FormGroup wrapper. It will be an empty shell so far.
Instructions
Section titled “Instructions”-
Update the
src/app/task-form/task-form.tsfile:import { Component } from "@angular/core";import { FormGroup } from "@angular/forms";@Component({selector: "app-task-form",imports: [],templateUrl: "./task-form.html",styleUrl: "./task-form.css",})export class TaskForm {form = new FormGroup({});}
FormControl
Section titled “FormControl”The form is meant to include two inputs: the name and the description.
As FormControl is meant to handle a single input, we need to populate the new FormGroup with 2 FormControls.
To create a FormControl, we need to instantiate it the same way we did for the FormGroup: new FormControl().
We also need to pass it a default value as a parameter: new FormControl()''.
Here an empty string, but some situations will require to display a default real value in your HTML.
Instructions
Section titled “Instructions”-
Update the
src/app/task-form/task-form.tsfile:import { Component } from "@angular/core";import { FormGroup, FormControl } from "@angular/forms";@Component({selector: "app-task-form",imports: [],templateUrl: "./task-form.html",styleUrl: "./task-form.css",})export class TaskForm {form = new FormGroup({title: new FormControl(""),description: new FormControl(""),});}
Form HTML Binding
Section titled “Form HTML Binding”The form structure is defined but that’s only an unused property in the task-form.ts file.
We need to bind the form to the HTML inputs: once done, it’ll allow getting the value of the inputs upon clicking on the submit button.
It comes in multiple steps:
- add a new import,
ReactiveFormsModuleto benefit from directives for the HTML template. - use these directives to bind the whole form and each input
Instructions
Section titled “Instructions”-
Import ReactiveFormsModule the
src/app/task-form/task-form.tsfile:import { Component } from "@angular/core";import { FormGroup, FormControl, ReactiveFormsModule } from "@angular/forms";@Component({selector: "app-task-form",imports: [ReactiveFormsModule],// ...})export class TaskForm {// ...} -
Bind the whole form with the
formGroupdirective in thesrc/app/task-form/task-form.htmlfile:<form [formGroup]="form" class="flex flex-col gap-2 m-auto"><fieldset class="fieldset w-full"><legend class="fieldset-legend">Name</legend><input type="text" class="input w-full" placeholder="Type here" /></fieldset><fieldset class="fieldset"><legend class="fieldset-legend">Description</legend><textareaclass="textarea w-full h-24"placeholder="Description"></textarea></fieldset><button type="submit" class="btn btn-primary">Submit</button></form> -
Bind the individual
FormControlsby their name with theformControlNamedirectives in thesrc/app/task-form/task-form.htmlfile:<form [formGroup]="form" class="flex flex-col gap-2 m-auto"><fieldset class="fieldset w-full"><legend class="fieldset-legend">Name</legend><inputformControlName="title"type="text"class="input w-full"placeholder="Type here"/></fieldset><fieldset class="fieldset"><legend class="fieldset-legend">Description</legend><textareaformControlName="description"class="textarea w-full h-24"placeholder="Description"></textarea></fieldset><button type="submit" class="btn btn-primary">Submit</button></form>
In this chapter, you learned how to bind a form to a Reactive Forms in Angular. You learned how to define the form on Component side and how to use directives to bind this form on HTML side.