Angular Dynamic Components
Learn Angular Dynamic Components, including ViewContainerRef, createComponent(), ComponentRef, setInput(), Outputs, destroy/clear, lazy loading, dialog systems, dashboard widgets, plugin architecture, dynamic forms and tabs, enterprise patterns, performance, common mistakes, and interview questions.
Learning Objectives
By the end of this lesson, you will understand:
- What Dynamic Components are.
- Why Angular uses Dynamic Components.
- Static Components vs Dynamic Components.
- How Angular creates components at runtime.
- The role of
ViewContainerRef. - The
createComponent()API. - Dynamic component lifecycle.
- Passing Inputs dynamically.
- Listening to Outputs dynamically.
- Destroying dynamically created components.
- Lazy loading Dynamic Components.
- Building reusable Dialog systems.
- Dashboard Widget architecture.
- Plugin architecture.
- Dynamic Forms.
- Dynamic Tabs.
- Enterprise use cases.
- Performance optimization.
- Common mistakes.
- Advanced interview questions.
Introduction
Imagine you're building TechLearningPro.
A student clicks:
Open Quiz
Angular instantly opens a Quiz Component.
Later:
Open Certificate
Angular displays a Certificate Component.
Next:
Open AI Assistant
A completely different component appears.
Did Angular create all these components during application startup?
No.
Angular creates them only when needed.
This is called Dynamic Component Rendering.
What Are Dynamic Components?
Normally Angular components are declared inside templates.
Example:
<app-header></app-header><app-sidebar></app-sidebar><app-course-list></app-course-list>
These are Static Components.
Dynamic Components are created programmatically during runtime.
Architecture:
Application↓User Action↓Angular Creates Component↓Insert Into DOM↓Render UI
Static vs Dynamic Components
Static
<app-course-card></app-course-card>
Created during template rendering.
Dynamic
viewContainerRef.createComponent(...)
Created only when required.
Architecture
StaticHTML↓Angular↓Render-------------------------DynamicRuntime↓createComponent()↓Render
Real-Time Example
Imagine TechLearningPro Dashboard.
The page initially contains:
HeaderSidebarCourse List
User clicks:
Show AI Tutor
Angular creates:
AI Tutor Component
Only at that moment.
Why Use Dynamic Components?
Dynamic Components are useful when:
DialogsPopupsNotificationsWidgetsDashboardsPlugin SystemsFormsMicro FrontendsAI Chat Windows
Instead of loading everything upfront.
Angular Dynamic Component Architecture
User Click↓Business Logic↓ViewContainerRef↓createComponent()↓Component Instance↓DOM
Everything happens at runtime.
ViewContainerRef
The most important class is:
ViewContainerRef
It represents a location where Angular can insert Views or Components.
Think of it as an empty container.
Architecture:
Container↓Empty↓Create Component↓Insert Component
Preparing the Container
Template
<ng-container #container></ng-container>
Component
@ViewChild('container',{ read: ViewContainerRef })container!: ViewContainerRef;
Now Angular knows where to insert components.
createComponent()
Modern Angular uses:
container.createComponent(CourseComponent);
Architecture
ViewContainer↓createComponent()↓CourseComponent↓DOM
First Dynamic Component
const component =this.container.createComponent(CourseComponent);
Immediately:
CourseComponent↓Visible
No HTML template required.
Component Instance
The returned object is:
ComponentRef<CourseComponent>
Architecture
ComponentRef↓Component Instance↓Methods↓Properties↓Destroy()
Passing Inputs
Suppose:
@Component(...)class CourseComponent{@Input()title!: string;}
Dynamic:
const ref =this.container.createComponent(CourseComponent);ref.setInput('title','Angular Signals');
Result:
Course Title↓Angular Signals
Listening to Outputs
Suppose:
@Output()close =new EventEmitter<void>();
Dynamic:
ref.instance.close.subscribe(() => {console.log('Closed');});
Architecture
Dynamic Component↓Output Event↓Parent↓Business Logic
Destroying Components
Unused components consume memory.
Destroy:
ref.destroy();
Architecture
Component↓Destroy↓Memory Released
Clearing the Container
Instead of destroying individual components:
container.clear();
Everything disappears.
Architecture
Container↓Clear()↓Empty
Dynamic Dialog System
Most Angular dialog libraries work using Dynamic Components.
Architecture
User Click↓Dialog Service↓Dynamic Component↓Dialog Window
Examples:
Login DialogSettingsCourse PreviewCertificatePayment
Building Your Own Dialog Service
Component↓Dialog Service↓ViewContainer↓Dialog Component↓User
This avoids hardcoding dialogs into templates.
Dashboard Widgets
Imagine TechLearningPro Admin Dashboard.
Users choose widgets:
RevenueUsersCoursesAI UsagePerformance
Architecture
Dashboard↓Selected Widgets↓Dynamic Components↓Dashboard Layout
Widgets appear only when selected.
Plugin Architecture
Suppose companies build plugins.
Plugin APlugin BPlugin C
Angular loads:
Only Selected Plugin
Architecture
Application↓Plugin Loader↓Dynamic Component↓UI
This is common in enterprise software.
Dynamic Forms
Suppose backend returns:
[{"type":"textbox"},{"type":"dropdown"},{"type":"checkbox"}]
Angular dynamically creates:
TextboxDropdownCheckbox
Architecture
Backend↓JSON↓Dynamic Components↓Form
Dynamic Tabs
Instead of:
ProfileCoursesSettings
being hardcoded.
Angular creates:
Tabs↓Dynamic Components↓Content
Useful for enterprise portals.
Lazy Loading Dynamic Components
Large applications shouldn't bundle every component.
Instead:
User Opens AI Tutor↓Lazy Load↓Component↓Render
Only required code is downloaded.
TechLearningPro Architecture
User↓Dashboard↓Widget Manager↓Dynamic Components↓QuizAI TutorCertificateCourse Preview
Everything loads on demand.
Enterprise Plugin Architecture
Backend↓Plugin Metadata↓Plugin Loader↓Dynamic Component↓User Interface
Banks, CRMs and ERP systems commonly use this architecture.
Performance Optimization
Prefer:
Lazy ComponentsDestroy Unused ComponentsReuse ContainersSmall ComponentsSignalsOnPush
Avoid:
Create HundredsNever DestroyLarge ComponentsHeavy Initialization
Common Mistakes
Forgetting destroy()
Memory leaks occur.
Creating Too Many Components
Only create what the user needs.
Massive Dynamic Components
Split them into smaller reusable components.
Business Logic Inside Components
Move logic into:
ServicesStoresSignals
Real-Time Example
TechLearningPro
Student clicks:
Take Quiz
Architecture
Course Page↓Quiz Button↓Quiz Service↓Dynamic Quiz Component↓Questions↓Result
No unnecessary components are loaded initially.
Enterprise Architecture
Backend↓Metadata↓Component Factory↓Dynamic Component↓Signals↓UI
This architecture scales extremely well.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat is a Dynamic Component?+
Answer
2BeginnerQuestionWhy use Dynamic Components?+
Answer
3BeginnerQuestionWhich Angular API creates Dynamic Components?+
Answer
ViewContainerRef.createComponent()4IntermediateQuestionWhat is ViewContainerRef?+
Answer
5IntermediateQuestionHow do you pass data to a Dynamic Component?+
Answer
componentRef.setInput(...) or set properties on the component instance where appropriate.6IntermediateQuestionHow do you listen for events from a Dynamic Component?+
Answer
@Output() EventEmitter (or other exposed event source) through the component instance.7IntermediateQuestionWhy should Dynamic Components be destroyed?+
Answer
8IntermediateQuestionWhat are common real-world uses of Dynamic Components?+
Answer
- Dialogs
- Notifications
- Dashboards
- Dynamic forms
- Widget systems
- Plugin architectures
- AI chat panels
- CMS page builders
9AdvancedQuestionCan Dynamic Components be lazy loaded?+
Answer
10AdvancedQuestionWhat is the recommended enterprise architecture?+
Answer
Summary
Dynamic Components allow Angular applications to create UI only when it's actually needed.
User Action│▼Business Logic│▼ViewContainerRef│▼createComponent()│▼Dynamic Component│▼Signals / Inputs / Outputs│▼Rendered UI
For TechLearningPro, this approach is ideal for features like AI Tutor, Quiz Engine, Certificate Viewer, Course Preview dialogs, and Admin dashboard widgets because components are instantiated only when the user requests them.
The key principle: The key principle: Use Dynamic Components for UI that is optional, configurable, or loaded on demand. Combine them with lazy loading, Signals, and OnPush to build enterprise-grade Angular applications that are fast, modular, and easy to extend.
Next Lesson
Angular Advanced Dependency Injection (DI) — You'll learn:
- Angular DI internals.
- Injector hierarchy.
- Environment Injector.
- Root vs Platform vs Component injectors.
providedInoptions (root,platform,any).inject()API.- Injection Tokens.
- Multi Providers.
- Factory Providers.
- Value Providers.
- Class Providers.
- Optional, Self, SkipSelf, and Host decorators.
- Tree-shakable providers.
- Route-level providers.
- Feature-level injectors.
- Plugin-based DI architecture.
- Enterprise DI patterns.
- Advanced interview questions.