Skip to content
Briebug

Developer Resource Center

Sharing our recent findings, expertise, and insights with the community.

How do I bind to 'formGroup' when it isn't a known property of 'form'?

Angular Testing Modules Frontend by Updated 5 min read

Short answer

The error means the template uses [formGroup] without the directive that defines it, which comes from ReactiveFormsModule. In a standalone component, add ReactiveFormsModule to the component's own imports array. In a component declared in an NgModule, import ReactiveFormsModule in that same module; importing it only in AppModule does not reach feature modules.

import { ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-settings-form',
  imports: [ReactiveFormsModule], // makes [formGroup] known
  template: `<form [formGroup]="form">...</form>`,
})
export class SettingsFormComponent {
  form = new FormGroup({ displayName: new FormControl('') });
}

What?

This error is a common occurrence regardless of your experience as an Angular developer. It’s not uncommon to have forms working in one area of your application only to find they don’t work in another. Perhaps you’re eager to build your template form. You create your component’s template, wire up the form controls and suddenly you’re met with this warning.

Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’.

This message indicates that Angular is unable to bind the component property form to the [formGroup] input since it’s not a valid HTML attribute. More importantly, it’s not a known Angular input. At least not yet and that’s the root of the problem. The [formGroup] directive comes from ReactiveFormsModule (FormsModule provides ngModel and the template-driven directives instead), so the solution is to import ReactiveFormsModule where the component’s template can see it: in a standalone component’s own imports, or in the NgModule that declares the component.

The fix in a standalone component

Components are standalone by default since Angular 19, and new projects have generated standalone components since Angular 17. A standalone component can only use the directives listed in its own imports array, so add ReactiveFormsModule there:

import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-settings-form',
  imports: [ReactiveFormsModule],
  template: `
    <form [formGroup]="form" (ngSubmit)="save()">
      <input formControlName="displayName" />
      <button type="submit">Save</button>
    </form>
  `,
})
export class SettingsFormComponent {
  form = new FormGroup({
    displayName: new FormControl('', { nonNullable: true }),
  });

  save() {
    console.log(this.form.getRawValue());
  }
}

Importing ReactiveFormsModule somewhere else, such as a parent component or app.config.ts, does not help: each standalone component must import what its own template uses. The same applies in unit tests. A standalone component brings its imports into TestBed with it, while a component declared in an NgModule needs ReactiveFormsModule in the test module’s imports.

It’s all about the Modules

If your component is declared in an NgModule rather than standalone, the same rule applies one level up. Surprisingly, this is not a Form problem, per se, but a module declaration problem. An NgModule defines which components, directives and pipes the templates of its declared components can use, and that’s where this error originates. You need to import the forms module in the same module that declares the component using the Angular Forms directives.

A discerning reader might wonder why there isn’t a forRoot() method for the forms module since both Forms modules declare providers. Often providers are utilized as singletons, as in there’s only ever one instance of a provider or service running during an application’s lifetime. When modules declare components, pipes, directives and providers, a forRoot() method in the module registers the providers once. Then the components, pipes, and directives are registered as needed with a forChild() method or something similar. In this case, both form modules declare providers whose persistence is directly related to the components that use them. Their state exists only for that component and is not shared across the application, which removes the need to utilize the forRoot() pattern.

Form Module Types

ModuleDirectives & validatorsExample directives
ReactiveFormsModuleReactive directives & validatorsformGroup, formControlName, formControl, formArrayName
FormsModuleTemplate-driven directives & validatorsngModel, ngForm, ngModelGroup

Where does the Form module go?

If you have a component that’s declared in another module, you may see an error when trying to use Angular form controls in that template. Even if you’ve imported a form module at the root of your application, you’ll need to do it again in modules that declare their components. The form modules export their directives and must be imported in the same module that the component is declared.

Let’s look at an example application with two modules. The ReactiveFormsModule is already imported in the root app.module.ts, and the form is working correctly in the app.component.html. Now it’s time to add another template form in the settings-form.component.html, and that’s when we run into the error.

.
├── .vscode
│   └── settings.json
├── src
│   ├── app
│   │   ├── settings
│   │   │   ├── settings-form
│   │   │   │   ├── settings-form.component.css
│   │   │   │   ├── settings-form.component.html  // [formGroup] does NOT work
│   │   │   │   └── settings-form.component.ts
│   │   │   └── settings.module.ts
│   │   ├── app.component.css
│   │   ├── app.component.html  // [formGroup] works
│   │   ├── app.component.ts
│   │   ├── app.module.ts  // import: [ReactiveFormsModule]
│   │   └── shared.service.ts
├── angular.json
├── package.json
└── tsconfig.json

When Angular compiles a component’s template, it only knows the components, pipes and directives available in the module that declares the component, which, in this example, is the settings.module.ts. It does not look upwards to app.module.ts. To make the form directives available to your component in this example, import ReactiveFormsModule in the module that declares the component: settings.module.ts.

.
├── .vscode
│   └── settings.json
├── src
│   ├── app
│   │   ├── settings
│   │   │   ├── settings-form
│   │   │   │   ├── settings-form.component.css
│   │   │   │   ├── settings-form.component.html  // [formGroup] works
│   │   │   │   └── settings-form.component.ts
│   │   │   └── settings.module.ts  // import: [ReactiveFormsModule]
│   │   ├── app.component.css
│   │   ├── app.component.html  // [formGroup] works
│   │   ├── app.component.ts
│   │   ├── app.module.ts  // import: [ReactiveFormsModule]
│   │   └── shared.service.ts
├── angular.json
├── package.json
└── tsconfig.json

The fixed SettingsModule looks like this. Comment out ReactiveFormsModule and the error comes back:

// src/app/settings/settings.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { SettingsFormComponent } from './settings-form/settings-form.component';

@NgModule({
  declarations: [SettingsFormComponent],
  imports: [CommonModule, ReactiveFormsModule],
})
export class SettingsModule {}

Summary

  • Standalone component: add ReactiveFormsModule to the component’s imports
  • NgModule component: import ReactiveFormsModule in the module that declares the component
  • Or import a module that exports ReactiveFormsModule where the component is declared

It should make more sense what the original error message is trying to explain. Angular is trying to bind to an unknown property of the <form /> element. The [formGroup] input is provided by a directive in ReactiveFormsModule, and a template can only use directives that are in scope. The component starts its search in its own decorator and, if it is not standalone, ends at the module where it’s declared. Ultimately, ReactiveFormsModule needs to be imported at this level or be exported from another imported module. Ironically the solution to this form error doesn’t have much to do with forms. Arriving at this solution requires an understanding of modules and their dependencies, which will improve your ability to debug future problems with more certainty.