← Back to Angular, page 3

angular-0047 · Hard senior

How would you migrate an older NgModule-based Angular application to the modern style (standalone, control flow, signals), and what is the order of work?

Update Angular one major version at a time with ng update, then run the official schematics in a safe order: standalone migration, control flow, inject(), route lazy loading, and then optional signal inputs, outputs and queries. Commit after each step, run tests, and migrate by feature so the app stays releasable. Old and new styles can live side by side.

Full answer

A migration is a project, not a command. A safe plan:

  1. Get the tests and the build green first. Without tests a migration is guesswork.
  2. Update the framework. ng update @angular/core @angular/cli moves one major version at a time. Read the update guide for each version, fix deprecations, and keep third-party libraries compatible.
  3. Standalone. ng generate @angular/core:standalone has three modes, run in order: convert declarations to standalone, remove unnecessary NgModules, and switch the bootstrap to bootstrapApplication. Commit after each mode. Libraries that still export NgModules can be imported into standalone components.
  4. Control flow. ng generate @angular/core:control-flow rewrites *ngIf, *ngFor, and *ngSwitch to @if, @for, and @switch. Review the generated track expressions. Then ng generate @angular/core:cleanup-unused-imports removes imports that are no longer needed.
  5. Dependency injection style. ng generate @angular/core:inject converts constructor parameters to inject().
  6. Routing. ng generate @angular/core:route-lazy-loading turns eager component routes into loadComponent. Convert class guards, resolvers, and interceptors to functions, and HttpClientModule to provideHttpClient().
  7. Signals (optional and gradual). signal-input-migration, output-migration, and signal-queries-migration convert @Input, @Output, and @ViewChild. Start with leaf components.
  8. Performance steps after that: OnPush, and then zoneless once the app is ready.

Keep a rule: new code uses the modern style, old code is converted when touched, or in planned batches. This avoids a big-bang rewrite. Review each migration diff, since schematics can produce code that compiles but needs manual tidying.

What does not need migration: NgModules still work and are still supported, and *ngIf still compiles in Angular 20 (it is deprecated). The team should decide by value: converting code that nobody changes has a low return.

typescript
// file: legacy/list.module.ts
import { CommonModule } from '@angular/common';
import { Component, Input, NgModule } from '@angular/core';

// LEGACY style: NgModule, decorator input, and structural directives.
@Component({
  selector: 'app-legacy-list',
  standalone: false,
  template: `
    <ul *ngIf="items.length > 0; else empty">
      <li *ngFor="let item of items; trackBy: trackById">{{ item.name }}</li>
    </ul>
    <ng-template #empty><p>No items</p></ng-template>
  `,
})
export class LegacyListComponent {
  @Input() items: { id: number; name: string }[] = [];

  trackById(_index: number, item: { id: number }): number {
    return item.id;
  }
}

@NgModule({
  declarations: [LegacyListComponent],
  imports: [CommonModule],
  exports: [LegacyListComponent],
})
export class LegacyListModule {}

// file: modern/list.component.ts
import { Component, input } from '@angular/core';

// MODERN style: standalone, signal input, and built-in control flow.
@Component({
  selector: 'app-list',
  template: `
    @if (items().length > 0) {
      <ul>
        @for (item of items(); track item.id) {
          <li>{{ item.name }}</li>
        }
      </ul>
    } @else {
      <p>No items</p>
    }
  `,
})
export class ListComponent {
  readonly items = input<{ id: number; name: string }[]>([]);
}

// file: modern/page.component.ts
import { Component } from '@angular/core';
import { LegacyListModule } from '../legacy/list.module';
import { ListComponent } from './list.component';

// Both styles can live in the same page during a gradual migration.
@Component({
  selector: 'app-page',
  imports: [LegacyListModule, ListComponent],
  template: `
    <app-legacy-list [items]="data" />
    <app-list [items]="data" />
  `,
})
export class PageComponent {
  data = [{ id: 1, name: 'Asha' }];
}

Angular 20.3, type-checked. The file shows one list component in the old style (legacy, kept compiling on purpose) and the same screen in the modern style.

Key points

  • Update with `ng update`, one major version at a time, with a green test suite.
  • Run schematics in order: standalone, control flow, inject, route lazy loading, signal APIs.
  • Commit per step and migrate by feature. Old and new can coexist.
  • Convert class-based guards, resolvers, and interceptors to functions.

Common mistakes

  • Updating several major versions at once and then being unable to find what broke.
  • Running every schematic in one commit, so the diff cannot be reviewed.
  • Rewriting stable, untouched modules for style only.

Follow-ups

  • How would you keep two teams productive while the migration runs?
  • Which migration gives the highest return first?
  • How do you handle a library that does not support standalone?

Tips

  • Give the order and the reason for it. It shows you have done or planned it.
  • Mention tests first, then small commits.
  • Say that you would not migrate everything at once and explain how you decide.

Evaluation rubric

  • Gives a safe, ordered plan with real schematic names.
  • Understands coexistence of styles.
  • Considers risk and cost.