Bird
0
0

What will happen if you use a custom structural directive without injecting ViewContainerRef in its constructor?

medium📝 component behavior Q5 of 15
Angular - Advanced Patterns
What will happen if you use a custom structural directive without injecting ViewContainerRef in its constructor?
AThe directive will work normally without errors.
BAngular will throw a runtime error when trying to add or remove views.
CThe directive will only add elements but not remove them.
DThe directive will only remove elements but not add them.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of ViewContainerRef

    ViewContainerRef is needed to insert or remove embedded views dynamically in the DOM.
  2. Step 2: Consequence of missing ViewContainerRef

    Without it, the directive cannot manipulate the DOM structure, causing runtime errors when calling methods like createEmbeddedView or clear.
  3. Final Answer:

    Angular will throw a runtime error when trying to add or remove views. -> Option B
  4. Quick Check:

    Missing ViewContainerRef = runtime error [OK]
Quick Trick: ViewContainerRef is essential for DOM manipulation in structural directives [OK]
Common Mistakes:
  • Assuming directive works without ViewContainerRef
  • Thinking it partially works (only add or remove)
  • Confusing with TemplateRef usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes