NgModules were used to organize Angular apps. Now, Angular uses standalone components to make code simpler and faster.
Migrating from NgModules in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { Component } from '@angular/core'; @Component({ selector: 'app-example', standalone: true, template: `<h1>Hello from standalone component!</h1>`, imports: [] }) export class ExampleComponent {}
Use standalone: true to mark a component as standalone.
Use imports array to add other standalone components, directives, or pipes.
import { Component } from '@angular/core'; @Component({ selector: 'app-hello', standalone: true, template: `<p>Hello World!</p>` }) export class HelloComponent {}
*ngFor.import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-list', standalone: true, imports: [CommonModule], template: `<ul><li *ngFor="let item of items">{{item}}</li></ul>` }) export class ListComponent { items = ['Apple', 'Banana', 'Cherry']; }
import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app.component'; bootstrapApplication(AppComponent);
This example shows a full Angular app using a standalone root component. It lists fruits using *ngFor from CommonModule. No NgModules are used.
import { Component } from '@angular/core'; import { bootstrapApplication } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-root', standalone: true, imports: [CommonModule], template: ` <h1>Welcome to Standalone Angular!</h1> <ul> <li *ngFor="let fruit of fruits">{{ fruit }}</li> </ul> ` }) export class AppComponent { fruits = ['Apple', 'Banana', 'Cherry']; } bootstrapApplication(AppComponent);
Standalone components replace the need for NgModules in many cases.
You can still use NgModules if needed, but standalone is the recommended modern way.
Remember to import necessary modules like CommonModule in standalone components to use Angular directives.
NgModules are no longer required; standalone components simplify Angular apps.
Use standalone: true and imports to build components without NgModules.
Bootstrap your app with bootstrapApplication() instead of platformBrowserDynamic().bootstrapModule().
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
