Discover how to make your Angular apps smarter by loading only what's needed, exactly when it's needed!
Why Dynamic component loading in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a dashboard where you must show different widgets based on user choices, but you have to write all possible widget HTML in advance and hide or show them manually.
Manually managing all widget HTML makes your code bulky, hard to maintain, and slow to update. Adding new widgets means changing many files and risking bugs.
Dynamic component loading lets Angular create and insert components on the fly, only when needed. This keeps your app fast, clean, and easy to extend.
if(showChart) { <chart-widget></chart-widget> } else { <table-widget></table-widget> }
loadComponent(widgetType) { this.container.createComponent(widgetType); }You can build flexible apps that load only what users need, improving performance and user experience.
A news app that loads different article layouts dynamically depending on the article type, without preloading all layouts.
Manual HTML for all components is hard to manage.
Dynamic loading creates components only when needed.
This makes apps faster, cleaner, and easier to grow.
Practice
Solution
Step 1: Understand dynamic component loading
Dynamic component loading means adding components to the app view during runtime, not just at compile time.Step 2: Compare options
Only To add components to the view while the app is running describes adding components while the app runs. Other options describe static or unrelated concepts.Final Answer:
To add components to the view while the app is running -> Option AQuick Check:
Dynamic loading = add components at runtime [OK]
- Confusing dynamic loading with static template declaration
- Thinking it changes CSS or modules
- Assuming it replaces Angular modules
Solution
Step 1: Identify the service for dynamic insertion
ViewContainerRef provides a container where components can be dynamically added or removed.Step 2: Eliminate other options
Renderer2 is for DOM manipulation, HttpClient for HTTP calls, NgModuleRef for module references, none for dynamic component insertion.Final Answer:
ViewContainerRef -> Option CQuick Check:
Dynamic insertion uses ViewContainerRef [OK]
- Choosing Renderer2 which is for DOM, not components
- Confusing HttpClient with component loading
- Using NgModuleRef incorrectly
const componentRef = viewContainerRef.createComponent(MyComponent); componentRef.instance.title = 'Hello';
Assuming
MyComponent displays {{ title }} in its template.Solution
Step 1: Understand createComponent and instance property
createComponent adds the component to the view. Settinginstance.titleassigns the property used in the template.Step 2: Predict rendered output
Since{{ title }}is displayed, andtitleis set to 'Hello', the text 'Hello' will appear.Final Answer:
The text 'Hello' will appear where the component is loaded -> Option BQuick Check:
Set instance property = visible text [OK]
- Thinking component is not attached after createComponent
- Assuming property must be declared differently
- Ignoring that instance properties affect template
@ViewChild('container', { read: ViewContainerRef }) containerRef!: ViewContainerRef;
load() {
const comp = this.containerRef.createComponent(SomeComponent);
comp.instance.data = 'Test';
}Solution
Step 1: Check ViewChild usage
ViewChild with#containerexpects a matching template reference variable in HTML.Step 2: Identify missing template reference
If#containeris missing in the template,containerRefwill be undefined, causing runtime errors.Final Answer:
Missing#containertemplate reference in HTML -> Option DQuick Check:
ViewChild needs matching template ref [OK]
- Assuming ComponentFactoryResolver is needed in Angular 14+
- Thinking instance properties can't be assigned
- Misusing read option in ViewChild
Solution
Step 1: Understand dynamic component switching
Using ViewContainerRef with createComponent allows loading different components at runtime by clearing old ones and adding new.Step 2: Evaluate other options
Declare all possible components in the template and use *ngIf to show/hide them is static and less flexible. Create components manually with new keyword and append to DOM bypasses Angular and breaks framework rules. Use Angular modules to switch components dynamically without ViewContainerRef misunderstands modules' role.Final Answer:
Use a single ViewContainerRef and callcreateComponent()with the chosen component type each time, clearing previous components -> Option AQuick Check:
Dynamic switching = clear + createComponent [OK]
- Using static *ngIf instead of dynamic loading
- Trying to create components with new keyword
- Confusing modules with dynamic component loading
