TExcelTAD
Home › Angular › Standalone vs NgModules

Standalone vs NgModules

Everything in this course uses standalone components: each component lists exactly what its template needs in its own imports array. Before standalone APIs existed, Angular organised code into NgModules. You will meet NgModules in older projects, libraries and tutorials, so it's worth understanding both — and how to move from one to the other.

The NgModule way

An NgModule is a class decorated with @NgModule that groups components and declares what they can use:

ts
// app.module.ts (legacy style)
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TaskCardComponent } from './task-card.component';
import { TruncatePipe } from './truncate.pipe';

@NgModule({
  declarations: [AppComponent, TaskCardComponent, TruncatePipe], // components that belong here
  imports: [BrowserModule, FormsModule],                          // modules whose exports we use
  providers: [],                                                  // services
  bootstrap: [AppComponent],                                      // root component
})
export class AppModule {}

// main.ts
platformBrowserDynamic().bootstrapModule(AppModule);

Every component in declarations can use every other declared component, plus anything exported by the imported modules. Components belonged to exactly one module and had standalone: false.

The standalone way

ts
// task-card.ts
@Component({
  selector: 'app-task-card',
  imports: [FormsModule, TruncatePipe], // only what THIS template uses
  template: `...`,
})
export class TaskCard {}

// main.ts
bootstrapApplication(App, appConfig);
NgModules Standalone
Where dependencies are declared In the module, shared by all its declarations In each component's imports
Bootstrapping bootstrapModule(AppModule) bootstrapApplication(App, config)
App-wide providers providers / forRoot() in modules provideX() functions in app.config.ts
Lazy loading loadChildren → a module loadComponent or loadChildren → routes
Boilerplate An extra file per feature None
Default in new projects No longer Yes

Seeing the difference: who can use what?

The core difference is the scope in which a template looks up a tag. In a module, the scope is "everything declared or imported by my module". Standalone narrows it to "my own imports". This plain TypeScript model resolves tags both ways:

ts · try it yourself
interface ModuleDef { declarations: string[]; imports: string[]; }

const sharedModuleExports = ['app-button', 'app-icon'];

const featureModule: ModuleDef = {
  declarations: ['app-task-list', 'app-task-card', 'app-task-filter'],
  imports: ['SharedModule'],
};

function moduleScope(m: ModuleDef): Set<string> {
  return new Set([...m.declarations, ...sharedModuleExports]);
}

function canUse(scope: Set<string>, tag: string): string {
  return `${tag}: ${scope.has(tag) ? 'yes' : 'NOT a known element'}`;
}

const viaModule = moduleScope(featureModule);
const standaloneTaskCard = new Set(['app-icon']); // imports: [Icon]

console.log('NgModule scope for app-task-card');
['app-task-filter', 'app-button'].forEach((t) => console.log('  ' + canUse(viaModule, t)));
console.log('Standalone TaskCard');
['app-task-filter', 'app-icon'].forEach((t) => console.log('  ' + canUse(standaloneTaskCard, t)));
NgModule scope for app-task-card
  app-task-filter: yes
  app-button: yes
Standalone TaskCard
  app-task-filter: NOT a known element
  app-icon: yes

The standalone version is more explicit: you can read a component file and know all its dependencies, and build tools can tree-shake and lazy-load more precisely.

Mixing both

The two styles interoperate:

  • A standalone component can import an NgModule: imports: [SomeLegacyModule] (common with older UI libraries).
  • An NgModule can import a standalone component: imports: [TaskCard] — not declarations.
angular · try it yourself
import { Component, NgModule } from '@angular/core';

@Component({
  selector: 'app-legacy-banner',
  standalone: false,               // a module-based component
  template: `<div class="banner">📢 Maintenance on Sunday, 02:00–04:00</div>`,
  styles: `.banner { background: #fff4d6; padding: 10px 14px; border-radius: 6px; }`,
})
export class LegacyBanner {}

@NgModule({
  declarations: [LegacyBanner],
  exports: [LegacyBanner],
})
export class LegacyBannerModule {}

@Component({
  selector: 'app-root',
  imports: [LegacyBannerModule],   // standalone component importing an NgModule
  template: `<app-legacy-banner /><h2>Dashboard</h2>`,
})
export class App {}

Migrating an existing app

The CLI includes an automated migration that runs in three steps:

bash · try it yourself
ng generate @angular/core:standalone
? Choose the type of migration: (Use arrow keys)
❯ Convert all components, directives and pipes to standalone
  Remove unnecessary NgModule classes
  Bootstrap the application using standalone APIs

Run the steps one at a time, build and test between them. Afterwards, ng generate @angular/core:control-flow and the signal input migrations modernise templates and inputs too.

Tip: Since Angular 19, components are standalone by default — the standalone: true flag you see in some code from 2023–2024 is no longer needed. You only write standalone: false for module-based components.

Test yourself

In a standalone component, where do you list the components its template uses?

  1. In an NgModule's declarations
  2. In the component's own imports array
  3. In app.config.ts
  4. In angular.json

How do you bootstrap a standalone application?

  1. platformBrowserDynamic().bootstrapModule(AppModule)
  2. bootstrapApplication(App, appConfig)
  3. new App().start()
  4. ng.bootstrap(App)

Can a standalone component use a component that is declared in an NgModule?

  1. No, never
  2. Yes, by importing the NgModule that exports it
  3. Only with standalone: true removed
  4. Only in tests