Discover how ditching NgModules can make your Angular code simpler and more fun to write!
Why Migrating from NgModules in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building an Angular app where every feature needs to be manually added to NgModules. You have to remember to import, declare, and export components everywhere.
This manual setup is slow and confusing. It's easy to forget imports or declarations, causing errors that are hard to track. The app structure becomes bulky and hard to maintain.
Migrating from NgModules to standalone components and signals lets Angular handle dependencies automatically. You write less boilerplate and focus on building features.
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MyComponent } from './my.component'; @NgModule({ declarations: [MyComponent], imports: [CommonModule] }) export class MyModule {}
import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ standalone: true, imports: [CommonModule], selector: 'my-component', template: '' }) export class MyComponent {}
This migration enables faster development with cleaner code and easier app scaling.
When adding a new feature, you no longer update multiple NgModules. Just create a standalone component and use it directly, saving time and reducing bugs.
Manual NgModule management is error-prone and slow.
Standalone components simplify dependencies and reduce boilerplate.
Migrating improves app maintainability and developer happiness.
Practice
Solution
Step 1: Understand NgModules role
NgModules group components and services but add complexity.Step 2: Benefits of standalone components
Standalone components remove NgModules, making the app simpler and easier to maintain.Final Answer:
Simplifies the app by removing the need for NgModules -> Option AQuick Check:
Standalone components simplify Angular apps by removing NgModules [OK]
- Thinking standalone components require more code
- Believing NgModules improve app speed
- Confusing standalone with class-based restriction
Solution
Step 1: Identify standalone component syntax
The correct property isstandalone: trueinside the@Componentdecorator.Step 2: Check other options
The other options are incorrect: one setsstandalone: false, another uses invalidmoduleproperty, and one misuses@NgModule.Final Answer:
@Component({ standalone: true, selector: 'app-hello', template: '<p>Hello</p>' }) -> Option DQuick Check:
standalone: trueinside@Componentdecorator [OK]
- Using @NgModule instead of @Component for standalone
- Setting standalone to false or missing it
- Confusing module property with standalone
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `{{ title }}
`
})
export class AppComponent {
title = 'Hello Angular Standalone';
}
bootstrapApplication(AppComponent);Solution
Step 1: Understand bootstrapApplication usage
UsingbootstrapApplicationwith a standalone component boots the app correctly.Step 2: Check component template and data
The template shows<h1>{{ title }}</h1>and title is set to 'Hello Angular Standalone'.Final Answer:
<h1>Hello Angular Standalone</h1> -> Option CQuick Check:
bootstrapApplication with standalone component renders {{ title }} [OK]
- Expecting bootstrapModule instead of bootstrapApplication
- Assuming title is undefined without constructor
- Thinking template won't render without NgModule
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `Welcome
`
})
export class AppComponent {}
bootstrapApplication(AppComponent, {
providers: []
});Solution
Step 1: Check standalone component dependencies
If the component uses other modules or components, they must be listed in theimportsarray inside@Component.Step 2: Analyze given code
The component has no imports array. The template uses*ngIf, which requiresCommonModuleinimports.Final Answer:
Missing imports array for dependencies in @Component -> Option BQuick Check:
Standalone components needimports: [CommonModule]for directives like *ngIf [OK]
- Thinking bootstrapApplication needs bootstrapModule
- Believing providers are disallowed in standalone
- Assuming selector rules changed for standalone
1. UsebootstrapApplication()instead ofplatformBrowserDynamic().bootstrapModule()2. Addstandalone: trueto components 3. Useimportsarray inside@Componentfor dependencies 4. KeepNgModuledeclarations as before
Solution
Step 1: Replace bootstrap method
UsebootstrapApplication()to start the app without NgModules.Step 2: Convert components to standalone
Addstandalone: trueto components to remove NgModule dependency.Step 3: Manage dependencies with imports
Useimportsarray inside@Componentto include needed modules or components.Step 4: Remove NgModules
NgModules are no longer needed and should be removed for full migration.Final Answer:
Apply steps 1, 2, and 3; remove NgModules completely -> Option AQuick Check:
bootstrapApplication + standalone: true + imports array, no NgModules [OK]
- Trying to keep NgModules with bootstrapApplication
- Forgetting to add standalone: true to components
- Not using imports array for dependencies
