Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is dynamic component loading in Angular?
Dynamic component loading means creating and inserting components into the app at runtime, not just when the app starts. It lets you add parts of the UI only when needed.
Click to reveal answer
beginner
Which Angular service is commonly used to load components dynamically?
The ViewContainerRef service is used to create and insert components dynamically into the view.
Click to reveal answer
intermediate
What is the role of ComponentFactoryResolver in dynamic component loading?
It helps create a factory for the component you want to load dynamically. This factory is then used to create the component instance.
Click to reveal answer
intermediate
How do you clean up a dynamically loaded component in Angular?
You call the destroy() method on the component reference to remove it and free resources.
Click to reveal answer
beginner
Why use dynamic component loading instead of static components?
It improves performance by loading components only when needed, and allows flexible UI changes without rebuilding the app.
Click to reveal answer
Which Angular directive is used as a placeholder to insert a dynamic component?
A<ng-template>
B<div>
C<component>
D<router-outlet>
✗ Incorrect
The directive acts as a placeholder where dynamic components can be inserted.
What method on ViewContainerRef inserts a component dynamically?
AinsertComponent()
BappendChild()
CloadComponent()
DcreateComponent()
✗ Incorrect
The createComponent() method creates and inserts a component dynamically into the container.
Which Angular version introduced the simplified dynamic component loading without ComponentFactoryResolver?
AAngular 9
BAngular 13
CAngular 14
DAngular 16
✗ Incorrect
Angular 14 introduced a simpler API for dynamic component loading without needing ComponentFactoryResolver.
What is the type of object returned when you create a dynamic component?
AComponentRef
BElementRef
CViewRef
DTemplateRef
✗ Incorrect
Creating a dynamic component returns a ComponentRef, which represents the created component instance.
Which Angular feature helps to pass data to a dynamically loaded component?
ARouter parameters
BInput properties
CDirect DOM manipulation
DServices only
✗ Incorrect
Input properties can be set on the ComponentRef instance to pass data to the dynamic component.
Explain how to load a component dynamically in Angular step-by-step.
Think about the services and methods Angular provides for dynamic loading.
You got /5 concepts.
Describe benefits and use cases for dynamic component loading in Angular apps.
Consider why you might not want to load all components at once.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of dynamic component loading in Angular?
easy
A. To add components to the view while the app is running
B. To statically declare all components in the template
C. To improve CSS styling of components
D. To replace Angular modules with components
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 A
Quick Check:
Dynamic loading = add components at runtime [OK]
Hint: Dynamic loading means adding components during app run [OK]
Common Mistakes:
Confusing dynamic loading with static template declaration
Thinking it changes CSS or modules
Assuming it replaces Angular modules
2. Which Angular service is used to insert a dynamic component into the view?
easy
A. Renderer2
B. HttpClient
C. ViewContainerRef
D. NgModuleRef
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 C
Quick Check:
Dynamic insertion uses ViewContainerRef [OK]
Hint: Use ViewContainerRef to insert dynamic components [OK]
Common Mistakes:
Choosing Renderer2 which is for DOM, not components
Confusing HttpClient with component loading
Using NgModuleRef incorrectly
3. Given this code snippet, what will be the output in the browser?
B. Using createComponent without importing ComponentFactoryResolver
C. Cannot assign data property on instance
D. Missing #container template reference in HTML
Solution
Step 1: Check ViewChild usage
ViewChild with #container expects a matching template reference variable in HTML.
Step 2: Identify missing template reference
If #container is missing in the template, containerRef will be undefined, causing runtime errors.
Final Answer:
Missing #container template reference in HTML -> Option D
Quick Check:
ViewChild needs matching template ref [OK]
Hint: Always add matching #ref in template for ViewChild [OK]
Common Mistakes:
Assuming ComponentFactoryResolver is needed in Angular 14+
Thinking instance properties can't be assigned
Misusing read option in ViewChild
5. You want to load different components dynamically based on user input. Which approach correctly handles this scenario in Angular?
hard
A. Use a single ViewContainerRef and call createComponent() with the chosen component type each time, clearing previous components
B. Declare all possible components in the template and use *ngIf to show/hide them
C. Create components manually with new keyword and append to DOM
D. Use Angular modules to switch components dynamically without ViewContainerRef
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 call createComponent() with the chosen component type each time, clearing previous components -> Option A
Quick Check:
Dynamic switching = clear + createComponent [OK]
Hint: Clear container then create chosen component dynamically [OK]