Sub topic 2
Importing ReactiveFormsModule
This chapter assumes you know TypeScript basics, Angular components and templates, standalone components, and the inject() function. It covers one narrow job: making the reactive forms directives available to your templates. You will see where the import goes, what it actually brings in, which parts of @angular/forms work without it, and the errors you get when it is missing or misplaced.
What the Module Contains
ReactiveFormsModule is an Angular NgModule exported from @angular/forms. It bundles a set of directives that link your component's form model to DOM elements. The form model classes themselves (FormControl, FormGroup, FormArray, FormRecord, and FormBuilder) are ordinary TypeScript classes and services. You can create and use them without importing the module at all.
The module matters only when a template has to talk to the model. These directives are the template-facing parts:
| Directive | Selector | Purpose |
|---|---|---|
| `FormGroupDirective` | `[formGroup]` | Binds a top-level `FormGroup` to a `<form>` or container |
| `FormControlName` | `[formControlName]` | Binds an input to a named child of the parent group |
| `FormControlDirective` | `[formControl]` | Binds an input to a standalone `FormControl` instance |
| `FormGroupName` | `[formGroupName]` | Binds a nested group by name |
| `FormArrayName` | `[formArrayName]` | Binds a `FormArray` by name |
| Built-in value accessors | `input`, `select`, `textarea`, etc. | Translate between DOM events and control values |
| `NgControlStatus` | any bound control | Adds CSS classes such as `ng-valid` and `ng-touched` |
| `ɵNgNoValidate` | `form:not([ngNoForm]):not([ngNativeValidate])` | Adds `novalidate` to forms |
The last two come from an internal module that FormsModule and ReactiveFormsModule share. That shared module explains two side effects people notice: Angular adds novalidate to your <form> elements, so the browser's own validation bubbles stop appearing, and Angular puts state classes on your inputs. If you want native browser validation back, add the ngNativeValidate attribute to the form.
Importing in a Standalone Component
In current Angular, components are standalone by default, and each component declares what its own template uses. You add ReactiveFormsModule to that component's imports array:
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-newsletter',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<label>
Email
<input type="email" [formControl]="email" />
</label>
<p>Current value: {{ email.value }}</p>
`,
})
export class NewsletterComponent {
email = new FormControl('', { nonNullable: true });
}To run it, bootstrap the component as usual. Forms need no extra application providers:
import { bootstrapApplication } from '@angular/platform-browser';
import { NewsletterComponent } from './newsletter.component';
bootstrapApplication(NewsletterComponent).catch((err) => console.error(err));The import is scoped to the component. If a child component also binds form directives in its own template, the child must import ReactiveFormsModule too. Importing it in the parent does not make the directives available to the child's template.
Importing in an NgModule-Based App
Older code declares components inside an NgModule. There you import ReactiveFormsModule into the module that declares the component:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, ReactiveFormsModule],
bootstrap: [AppComponent],
})
export class AppModule {}Module imports do not pass down to other modules. A feature module needs its own import, or an import of a shared module that re-exports ReactiveFormsModule in its exports array.
Configuring the Module with withConfig
ReactiveFormsModule.withConfig() returns a configured version of the module. It accepts two options:
| Option | Values | Default | Effect |
|---|---|---|---|
| `warnOnNgModelWithFormControl` | `'always'`, `'once'`, `'never'` | `'once'` | Warns when `ngModel` is used on an element that also has `formControl` or `formControlName` |
| `callSetDisabledState` | `'always'`, `'whenDisabledForLegacyCode'` | `'always'` | Controls whether value accessors get `setDisabledState(false)` on initialization |
import { Component } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-legacy-field',
standalone: true,
imports: [
ReactiveFormsModule.withConfig({
warnOnNgModelWithFormControl: 'always',
callSetDisabledState: 'whenDisabledForLegacyCode',
}),
],
template: `<input [formControl]="name" />`,
})
export class LegacyFieldComponent {
name = new FormControl('', { nonNullable: true });
}Use 'whenDisabledForLegacyCode' only during a migration, when a custom value accessor breaks because Angular now calls setDisabledState even for enabled controls. New code should keep the default.
Common Errors and What They Mean
| Error message | Cause | Fix |
|---|---|---|
| `Can't bind to 'formGroup' since it isn't a known property of 'form'` | The template's component or module does not import `ReactiveFormsModule` | Add it to that component's `imports` |
| `Can't bind to 'formControl' since it isn't a known property of 'input'` | Same cause, standalone control | Same fix |
| `NG01050: formControlName must be used with a parent formGroup directive` | `formControlName` appears with no `[formGroup]` ancestor in the same template | Wrap it in an element bound with `[formGroup]`, or use `[formControl]` |
| `No value accessor for form control name: 'x'` | The bound element has no value accessor, often a custom component | Implement `ControlValueAccessor` (covered in a later chapter) |
| Console warning about `ngModel` with `formControl` | `FormsModule` and `ReactiveFormsModule` are both imported and mixed on one element | Pick one model per field; remove `ngModel` |
The first error is the one you will hit most often. It usually shows up after moving a template into a new child component and forgetting the import there.
Tests and the Module
A unit test that only builds a FormGroup and checks its validity needs no TestBed and no module import, because the model classes are plain TypeScript. A component test that renders a template needs the directives. With standalone components, the component's own imports carry ReactiveFormsModule, so TestBed.configureTestingModule({ imports: [NewsletterComponent] }) is enough.
Exercise
A standalone CheckoutComponent imports ReactiveFormsModule and renders <app-address-fields [group]="form.controls.address" />. Inside AddressFieldsComponent, the template contains <div [formGroup]="group"><input formControlName="street" /></div>, and the app fails with "Can't bind to 'formGroup' since it isn't a known property of 'div'". Why, and what is the fix?
Show answer
Standalone imports apply only to the component that declares them. CheckoutComponent's import does not reach AddressFieldsComponent's template. Add ReactiveFormsModule to AddressFieldsComponent's own imports array.
Exercise
Your form shows no browser validation popups even though the inputs have required and type="email". Nothing in your code adds novalidate. Where does it come from, and how can you restore native validation?
Show answer
The forms modules include a directive (ɵNgNoValidate) that adds novalidate to <form> elements automatically. Add the ngNativeValidate attribute to the <form> to opt out and get native browser validation back.
Exercise
Does a Jasmine test that creates new FormGroup({ name: new FormControl('') }) and asserts on form.valid need to import ReactiveFormsModule? Explain.
Show answer
No. FormGroup and FormControl are plain classes from @angular/forms. The module only supplies template directives and value accessors, which a model-only test never uses.