Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Dynamic Components

AngularLesson 4225 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Dynamic Components
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Dynamic Components, in plain language

Dynamic Components can be created programmatically at runtime.

Problem → Why → Solution

The problem — why this concept matters

Failing to destroy a dynamic component reference can retain views and subscriptions.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Dynamic components are useful for dialogs, configurable dashboards, and plugin-like UI where the component type is chosen at runtime. Prefer declarative control flow when sufficient; imperative creation must coordinate inputs, outputs, injector, host location, and destruction. Never load an untrusted component type or configuration without an allowlist.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Dynamic Components

Core syntax or pattern

typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>

Dynamic components are useful for dialogs, configurable dashboards, and plugin-like UI where the component type is chosen at runtime. Prefer declarative control flow when sufficient; imperative creation must coordinate inputs, outputs, injector, host location, and destruction. Never load an untrusted component type or configuration without an allowlist.

Read the code step by step

  1. input and state// dynamic-host.directive.ts

    Dynamic Components can be created programmatically at runtime.

  2. component and templatedynamicHost!: DynamicHostDirective;

    Dynamic components are useful for dialogs, configurable dashboards, and plugin-like UI where the component type is chosen at runtime. Prefer declarative control flow when sufficient; imperative creation must coordinate inputs, outputs, injector, host location, and destruction. Never load an untrusted component type or configuration without an allowlist.

  3. rendered Angular UI<button (click)="loadComponent()">Load Component</but...

    Build a widget registry that renders two allowlisted component types from configuration and test input updates, outputs, and removal cleanup.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of Dynamic Components

Failure

Failing to destroy a dynamic component reference can retain views and subscriptions.

Dynamic Components can be created programmatically at runtime.

typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>
You should see
(Clicking the button will dynamically create AlertComponent at runtime)

Real-world usage

How to apply it in a real project

Build a widget registry that renders two allowlisted component types from configuration and test input updates, outputs, and removal cleanup.

Mini challenge

Build it yourself

Rebuild the Dynamic Components example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// dynamic-host.directive.ts
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appDynamicHost]'
})
export class DynamicHostDirective {
  constructor(public viewContainerRef: ViewContainerRef) {}
}

// parent.component.ts
import { ComponentFactoryResolver, ViewChild } from '@angular/core';

export class ParentComponent {
  @ViewChild(DynamicHostDirective, { static: true }) 
  dynamicHost!: DynamicHostDirective;
  
  constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
  
  loadComponent() {
    const viewContainerRef = this.dynamicHost.viewContainerRef;
    viewContainerRef.clear();
    
    const componentRef = viewContainerRef.createComponent(AlertComponent);
    componentRef.instance.message = 'Dynamic Alert!';
  }
}

// parent.component.html
<div appDynamicHost></div>
<button (click)="loadComponent()">Load Component</button>

Expected: Explain the core concept and Angular data flow of Dynamic Components

Show hint

Build a widget registry that renders two allowlisted component types from configuration and test input updates, outputs, and removal cleanup.

Quick quiz 1 / 3

What best describes the core idea of Dynamic Components?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • Dynamic components are useful for dialogs, configurable dashboards, and plugin-like UI where the component type is chosen at runtime. Prefer declarative control flow when sufficient; imperative creation must coordinate inputs, outputs, injector, host location, and destruction. Never load an untrusted component type or configuration without an allowlist.
  • Failing to destroy a dynamic component reference can retain views and subscriptions.
  • Build a widget registry that renders two allowlisted component types from configuration and test input updates, outputs, and removal cleanup.

Angular — Components — Angular

Easy traps

  • Failing to destroy a dynamic component reference can retain views and subscriptions.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Dynamic Components example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (Clicking the button will dynamically create AlertComponent at runtime)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Dynamic Components | Thuta Learning