← Back to Angular, page 1

angular-0004 · Easy fresher

What are standalone components, and how do they differ from the older NgModule approach?

A standalone component declares its own dependencies in its imports array and needs no NgModule. NgModules are the older way, where components were declared in a module that listed what they could use. Standalone is the default for new code from Angular 19.

Full answer

Before Angular 14, every component had to belong to exactly one NgModule. The module's declarations listed the component, its imports listed other modules whose pieces the template could use, and its exports listed what other modules could see. Beginners found this confusing, because the template's dependencies were declared far from the template.

A standalone component skips the module. It lists what its template needs directly in its own imports array: other standalone components, directives, pipes, or even whole NgModules (for example FormsModule). Nothing else is needed.

The important differences:

  • Dependencies live next to the template, so the file is easier to read and to move.
  • Bundlers can remove unused code more easily, since there is no large shared module.
  • App-wide services are registered with provide... functions in ApplicationConfig, not in a root module.
  • Lazy loading works per component with loadComponent.

Version notes: standalone arrived as a developer preview in Angular 14 and was stable in 15. From Angular 19, standalone: true is the default, so you only write standalone: false for a component that must be declared in a module. NgModules are not removed, and old libraries still ship them. A standalone component can import an NgModule, and an NgModule can import a standalone component, so migration can be done step by step.

typescript
import { Component, NgModule } from '@angular/core';

// Legacy style: a component declared inside an NgModule.
@Component({
  selector: 'app-old-badge',
  standalone: false,
  template: `<span>Legacy badge</span>`,
})
export class OldBadgeComponent {}

@NgModule({
  declarations: [OldBadgeComponent],
  exports: [OldBadgeComponent],
})
export class OldBadgeModule {}

// Modern style: a standalone component that lists its own dependencies.
@Component({
  selector: 'app-page',
  imports: [OldBadgeModule],
  template: `
    <h1>Report card</h1>
    <app-old-badge />
  `,
})
export class PageComponent {}

Angular 20.3. The module part of this file is the legacy style, kept to show interoperability. A component in an NgModule must set `standalone: false` in Angular 19 and later.

Key points

  • Standalone: `imports` is on the component. NgModule: components go in `declarations` and the module lists `imports`.
  • From Angular 19, components are standalone by default; the flag is only written to opt out.
  • Standalone components can import NgModules, so both styles work together.
  • NgModules are legacy for new code but common in older projects.

Common mistakes

  • Putting a standalone component in `declarations`. It must go in `imports`.
  • Writing `standalone: true` in every component of an Angular 19 or 20 project. It is allowed but unnecessary.
  • Thinking NgModules are deleted from Angular. They are still supported.

Follow-ups

  • How would you migrate a large NgModule app step by step?
  • What does `importProvidersFrom` do in an application config?
  • How does the Angular CLI help with migration?

Tips

  • Give the reason for the change (simpler mental model, less boilerplate), not only the syntax.
  • Say clearly which style you would use in a new project and that you can still read NgModule code.
  • If you have maintained a legacy app, mention the `ng generate @angular/core:standalone` migration schematic.

Evaluation rubric

  • Explains where dependencies are declared in each style.
  • Knows the version history roughly (14, 15, 19).
  • Mentions interoperability.