Bird
0
0

Why does this custom structural directive fail to toggle content?

medium📝 Debug Q7 of 15
Angular - Advanced Patterns
Why does this custom structural directive fail to toggle content?
@Directive({ selector: '[appShow]' })
export class ShowDirective {
  constructor(private tpl: TemplateRef, private vcr: ViewContainerRef) {}

  @Input() set appShow(condition: boolean) {
    if (condition) {
      this.vcr.createEmbeddedView(this.tpl);
    }
  }
}
AIt never clears the view when condition is false.
BTemplateRef is not injected properly.
CThe selector is incorrect for structural directives.
DThe @Input property name does not match the selector.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the setter logic

    The setter only creates the view when condition is true but does not clear it when false.
  2. Step 2: Understand effect on toggling

    Without clearing the view, the content remains visible even if condition becomes false.
  3. Final Answer:

    It never clears the view when condition is false. -> Option A
  4. Quick Check:

    Missing clear() causes content to stay visible [OK]
Quick Trick: Always clear view container when condition is false [OK]
Common Mistakes:
  • Forgetting to clear the view on false condition
  • Confusing TemplateRef and ViewContainerRef roles
  • Incorrect selector syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes