Bird
0
0

Why does this dynamic component loading code cause an error?

medium📝 Debug Q7 of 15
Angular - Advanced Patterns
Why does this dynamic component loading code cause an error?
const componentRef = viewContainerRef.createComponent(undefined);
ABecause the component type is undefined, Angular cannot create it.
BBecause createComponent requires a string selector, not a type.
CBecause viewContainerRef is not injected properly.
DBecause createComponent only works with template references.
Step-by-Step Solution
Solution:
  1. Step 1: Check the argument passed to createComponent

    The argument is undefined, so Angular has no component type to create.
  2. Step 2: Understand Angular's requirement for createComponent

    Angular needs a valid component type or factory to create a component instance.
  3. Final Answer:

    Because the component type is undefined, Angular cannot create it. -> Option A
  4. Quick Check:

    Undefined component type causes creation error [OK]
Quick Trick: Always pass a valid component type to createComponent [OK]
Common Mistakes:
  • Thinking createComponent accepts string selectors
  • Assuming injection error without checking argument
  • Confusing createComponent with template reference usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes