Angular Tutorial 0/55 lessons ~6 min read Lesson 38

    Angular Standalone Components Deep Dive

    Since Angular 15+, standalone components are the default architecture: a component declares its own imports instead of living in an NgModule.

    Course progress0%
    Focus
    7 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Since Angular 15+, standalone components are the default architecture: a component declares its own imports instead of living in an NgModule. New CLI apps generate standalone by default. Modules still work for legacy code, but new features should be standalone-first.

    Purpose of this lesson

    You will author a standalone component, route it lazily, and know when a shared NgModule is still justified.

    Understanding the topic

    What standalone changes:

    • standalone: true (default in modern schematics) — the component owns its dependency list.
    • Import what the template needs: CommonModule pieces, other components, pipes, or RouterLink.
    • Bootstrap with bootstrapApplication(AppComponent, { providers: [...] }) — no AppModule required.
    • Lazy routes load a component (or a small feature set) without a feature NgModule.

    Informative example

    Standalone course card + lazy route:

    typescript
    import { Component, input } from "@angular/core";
    import { RouterLink } from "@angular/router";
    import { CurrencyPipe } from "@angular/common";
    @Component({
    selector: "tlp-course-card",
    imports: [RouterLink, CurrencyPipe],
    template: `
    <a [routerLink]="['/learn', slug()]">
    <h2>{{ title() }}</h2>
    <p>{{ price() | currency:'USD' }}</p>
    </a>
    `,
    })
    export class CourseCardComponent {
    title = input.required<string>();
    slug = input.required<string>();
    price = input(0);
    }
    // app.routes.ts
    export const routes = [
    {
    path: "learn/:slug",
    loadComponent: () =>
    import("./course-page.component").then((m) => m.CoursePageComponent),
    },
    ];

    Best practices

    • Prefer loadComponent / loadChildren with standalone APIs over feature NgModules.
    • Keep imports arrays small — extract shared UI into tiny standalone components.
    • Migrate module-based libraries at the boundary; do not rewrite everything at once.

    Common mistakes

    • Forgetting to import a pipe/directive the template uses — templates fail at compile time under strictTemplates.
    • Reintroducing a catch-all SharedModule that re-creates NgModule coupling.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    2 questions
    1BeginnerQuestionWhat problem do standalone components solve?+

    Answer

    They remove mandatory NgModule ceremony: each component declares its own imports, which simplifies lazy loading, tree-shaking, and mental models for new Angular apps.
    2IntermediateQuestionCan standalone and NgModule apps coexist?+

    Answer

    Yes. Standalone components can be declared in an NgModule's imports, and module-based components can be imported into standalone ones. Migration is incremental.
    Ready to mark this lesson complete?Track your journey across the entire course.