Bird
0
0

Why is it important to clear the ViewContainerRef before creating new embedded views in a custom structural directive?

hard📝 Conceptual Q10 of 15
Angular - Advanced Patterns
Why is it important to clear the ViewContainerRef before creating new embedded views in a custom structural directive?
ATo prevent memory leaks and duplicate views in the DOM.
BBecause Angular requires clearing before any DOM manipulation.
CTo reset the directive's internal state variables.
DTo trigger change detection manually.
Step-by-Step Solution
Solution:
  1. Step 1: Understand what happens without clearing

    If you don't clear the ViewContainerRef, old views remain, causing duplicates and memory issues.
  2. Step 2: Importance of clearing before adding views

    Clearing removes previous views so the DOM reflects the current state without duplicates or leaks.
  3. Final Answer:

    To prevent memory leaks and duplicate views in the DOM. -> Option A
  4. Quick Check:

    Clear ViewContainerRef to avoid duplicates and leaks [OK]
Quick Trick: Always clear before adding views to avoid duplicates [OK]
Common Mistakes:
  • Thinking clearing triggers change detection
  • Assuming Angular auto-clears views
  • Confusing clearing with resetting state

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes