Sub topic 1
Quick recap: reactive forms
This chapter refreshes the parts of reactive forms that FormArray builds on. The later chapters assume you are comfortable with everything here.
Prerequisites
You should already know the following:
- TypeScript basics: classes, generics, and optional chaining.
- Angular components and templates, including the
@ifand@forcontrol flow blocks. - Standalone components, and how to import
ReactiveFormsModuleinto one. - The names
FormControl,FormGroup, andValidators. This chapter reviews them but does not teach them from scratch.
The model lives in the class
In a reactive form, you create the form model in the component class. The template only binds to that model. This is the opposite of template-driven forms, where the template creates the model.
That design is why reactive forms suit dynamic forms. The model is plain TypeScript, so you can add, remove, or replace parts of it in code at any time.
The building blocks
Every form is a tree built from three kinds of control. All three extend AbstractControl, so they share the same properties: value, valid, errors, touched, dirty, and valueChanges.
| Class | Holds | Value type |
|---|---|---|
| `FormControl` | One field | A single value |
| `FormGroup` | Named children | An object keyed by child name |
| `FormArray` | Ordered, unnamed children | An array |
A group's validity depends on its children. If one child is invalid, the group is invalid, and so is every ancestor up to the root form. FormArray follows the same rule, and it is the next chapter's topic.
A complete example
This component has a group with two controls. It uses NonNullableFormBuilder, so reset() returns each control to its initial value instead of null.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-profile-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="save()">
<input formControlName="name" placeholder="Name" />
<input formControlName="email" placeholder="Email" />
@if (form.controls.email.touched && form.controls.email.errors?.['email']) {
<p>Enter a valid email address.</p>
}
<button type="submit" [disabled]="form.invalid">Save</button>
</form>
`,
})
export class ProfileFormComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
});
save() {
console.log(this.form.getRawValue());
}
}Three bindings connect the template to the model:
[formGroup]attaches the form element to the group.formControlNameattaches an input to the child with that key.form.controls.emailgives typed access to a child in the class and the template.
Common errors
- "Can't bind to 'formGroup' since it isn't a known property of 'form'." The component does not import
ReactiveFormsModule. - "Cannot find control with name: 'x'." The
formControlNamestring does not match a key in the group, or the input sits outside the[formGroup]element. - Missing values on submit.
form.valueleaves out disabled controls. UsegetRawValue()when you need every field. nullafterreset(). A plainFormControlresets tonull. UsenonNullable: true, orNonNullableFormBuilderas above.
Check your understanding
Exercise
A form has a FormGroup with the controls name (valid) and email (invalid). What is the value of form.valid, and why?
Show answer
It is false. A FormGroup is valid only when all of its enabled children are valid, so one invalid child makes the group invalid.
Exercise
You call form.value and notice that a disabled email field is missing from the result. Which method returns it, and which class property type would you expect for a plain FormControl<string> after reset()?
Show answer
form.getRawValue() includes disabled controls. A plain FormControl<string> becomes string | null and resets to null, unless you set nonNullable: true or use NonNullableFormBuilder.