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.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Dynamic Components
Core syntax or pattern
// 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
- input and state
// dynamic-host.directive.tsDynamic Components can be created programmatically at runtime.
- component and template
dynamicHost!: 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.
- 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.
- input and state
- component and template
- 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.
// 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>(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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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