Bird
Raised Fist0
Angularframework~10 mins

Migrating from NgModules in Angular - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a standalone component in Angular.

Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-hello',
  template: `<h1>Hello World</h1>`,
  standalone: [1]
})
export class HelloComponent {}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C'yes'
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' or string values instead of boolean true.
Omitting the 'standalone' property entirely.
2fill in blank
medium

Complete the code to import the CommonModule in a standalone component.

Angular
import { Component } from '@angular/core';
import { [1] } from '@angular/common';

@Component({
  selector: 'app-list',
  template: `<ul><li *ngFor="let item of items">{{item}}</li></ul>`,
  standalone: true,
  imports: [CommonModule]
})
export class ListComponent {
  items = ['One', 'Two', 'Three'];
}
Drag options to blanks, or click blank then click option'
AFormsModule
BBrowserModule
CCommonModule
DHttpClientModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BrowserModule instead of CommonModule in standalone components.
Forgetting to import CommonModule when using structural directives.
3fill in blank
hard

Fix the error in the standalone component by completing the imports array.

Angular
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-nav',
  template: `<a routerLink="/home">Home</a>`,
  standalone: true,
  imports: [[1]]
})
export class NavComponent {}
Drag options to blanks, or click blank then click option'
ABrowserModule
BRouterModule
CFormsModule
DHttpClientModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BrowserModule instead of RouterModule.
Not importing any module, causing template errors.
4fill in blank
hard

Fill both blanks to define a standalone component that uses FormsModule and declares a template variable.

Angular
import { Component } from '@angular/core';
import { [1] } from '@angular/forms';

@Component({
  selector: 'app-input',
  template: `<input #inputRef type="text" [(ngModel)]="name">`,
  standalone: true,
  imports: [[2]]
})
export class InputComponent {
  name = '';
}
Drag options to blanks, or click blank then click option'
AFormsModule
BCommonModule
CRouterModule
DHttpClientModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing CommonModule instead of FormsModule for ngModel.
Forgetting to add FormsModule to imports array.
5fill in blank
hard

Fill all three blanks to create a standalone component that imports CommonModule, uses a signal, and displays its value.

Angular
import { Component, signal } from '@angular/core';
import { [1] } from '@angular/common';

@Component({
  selector: 'app-counter',
  template: `<button (click)="increment()">Increment</button> <p *ngIf="count()">Count: {{ count() }}</p>`,
  standalone: true,
  imports: [[2]]
})
export class CounterComponent {
  count = [3](0);

  increment() {
    this.count.update(c => c + 1);
  }
}
Drag options to blanks, or click blank then click option'
ACommonModule
Csignal
DFormsModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using FormsModule instead of CommonModule for template directives.
Not using signal() to create reactive state.

Practice

(1/5)
1. What is the main benefit of migrating from NgModules to standalone components in Angular?
easy
A. Simplifies the app by removing the need for NgModules
B. Requires more boilerplate code for each component
C. Makes the app slower to load
D. Forces use of class-based components only

Solution

  1. Step 1: Understand NgModules role

    NgModules group components and services but add complexity.
  2. Step 2: Benefits of standalone components

    Standalone components remove NgModules, making the app simpler and easier to maintain.
  3. Final Answer:

    Simplifies the app by removing the need for NgModules -> Option A
  4. Quick Check:

    Standalone components simplify Angular apps by removing NgModules [OK]
Hint: Standalone means no NgModules needed, simpler setup [OK]
Common Mistakes:
  • Thinking standalone components require more code
  • Believing NgModules improve app speed
  • Confusing standalone with class-based restriction
2. Which of the following is the correct way to declare a standalone component in Angular?
easy
A. @Component({ standalone: false, selector: 'app-hello', template: '

Hello

' })
B. @Component({ module: true, selector: 'app-hello', template: '

Hello

' })
C. @NgModule({ standalone: true, declarations: [HelloComponent] })
D. @Component({ standalone: true, selector: 'app-hello', template: '

Hello

' })

Solution

  1. Step 1: Identify standalone component syntax

    The correct property is standalone: true inside the @Component decorator.
  2. Step 2: Check other options

    The other options are incorrect: one sets standalone: false, another uses invalid module property, and one misuses @NgModule.
  3. Final Answer:

    @Component({ standalone: true, selector: 'app-hello', template: '<p>Hello</p>' }) -> Option D
  4. Quick Check:

    standalone: true inside @Component decorator [OK]
Hint: Look for standalone: true inside @Component decorator [OK]
Common Mistakes:
  • Using @NgModule instead of @Component for standalone
  • Setting standalone to false or missing it
  • Confusing module property with standalone
3. Given this code snippet, what will be the output when the app runs?
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);
medium
A.

undefined

B. Error: bootstrapModule is required
C.

Hello Angular Standalone

D. Blank page with no content

Solution

  1. Step 1: Understand bootstrapApplication usage

    Using bootstrapApplication with a standalone component boots the app correctly.
  2. Step 2: Check component template and data

    The template shows <h1>{{ title }}</h1> and title is set to 'Hello Angular Standalone'.
  3. Final Answer:

    <h1>Hello Angular Standalone</h1> -> Option C
  4. Quick Check:

    bootstrapApplication with standalone component renders {{ title }} [OK]
Hint: bootstrapApplication runs standalone component, shows title [OK]
Common Mistakes:
  • Expecting bootstrapModule instead of bootstrapApplication
  • Assuming title is undefined without constructor
  • Thinking template won't render without NgModule
4. Identify the error in this code snippet migrating from NgModules:
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: [] });
medium
A. bootstrapApplication requires bootstrapModule instead
B. Missing imports array for dependencies in @Component
C. Standalone components cannot have providers
D. Selector 'app-root' is invalid for standalone components

Solution

  1. Step 1: Check standalone component dependencies

    If the component uses other modules or components, they must be listed in the imports array inside @Component.
  2. Step 2: Analyze given code

    The component has no imports array. The template uses *ngIf, which requires CommonModule in imports.
  3. Final Answer:

    Missing imports array for dependencies in @Component -> Option B
  4. Quick Check:

    Standalone components need imports: [CommonModule] for directives like *ngIf [OK]
Hint: Standalone components need imports for dependencies [OK]
Common Mistakes:
  • Thinking bootstrapApplication needs bootstrapModule
  • Believing providers are disallowed in standalone
  • Assuming selector rules changed for standalone
5. You want to migrate an Angular app from NgModules to standalone components. Which combination correctly replaces the traditional bootstrap method and module imports?
1. Use bootstrapApplication() instead of platformBrowserDynamic().bootstrapModule()
2. Add standalone: true to components
3. Use imports array inside @Component for dependencies
4. Keep NgModule declarations as before
hard
A. Apply steps 1, 2, and 3; remove NgModules completely
B. Only step 1 is needed; keep NgModules and declarations
C. Apply steps 2 and 4; bootstrapModule remains required
D. Use step 4 only; standalone components are optional

Solution

  1. Step 1: Replace bootstrap method

    Use bootstrapApplication() to start the app without NgModules.
  2. Step 2: Convert components to standalone

    Add standalone: true to components to remove NgModule dependency.
  3. Step 3: Manage dependencies with imports

    Use imports array inside @Component to include needed modules or components.
  4. Step 4: Remove NgModules

    NgModules are no longer needed and should be removed for full migration.
  5. Final Answer:

    Apply steps 1, 2, and 3; remove NgModules completely -> Option A
  6. Quick Check:

    bootstrapApplication + standalone: true + imports array, no NgModules [OK]
Hint: Use bootstrapApplication + standalone + imports, drop NgModules [OK]
Common Mistakes:
  • Trying to keep NgModules with bootstrapApplication
  • Forgetting to add standalone: true to components
  • Not using imports array for dependencies