Bird
0
0

You want to create a custom structural directive that repeats a template N times. Which approach correctly implements this behavior?

hard📝 Application Q9 of 15
Angular - Advanced Patterns
You want to create a custom structural directive that repeats a template N times. Which approach correctly implements this behavior?
ACreate multiple components dynamically instead of using embedded views.
BInject TemplateRef and ViewContainerRef, then loop N times calling createEmbeddedView each iteration.
CUse a service to generate an array of N elements and bind with *ngFor inside the directive.
DUse Renderer2 to clone the element N times manually.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to repeat templates in structural directives

    Repeating means creating multiple embedded views of the same template.
  2. Step 2: Use ViewContainerRef to create views in a loop

    Inject TemplateRef and ViewContainerRef, then loop N times calling createEmbeddedView to render the template multiple times.
  3. Final Answer:

    Inject TemplateRef and ViewContainerRef, then loop N times calling createEmbeddedView each iteration. -> Option B
  4. Quick Check:

    Repeat template = loop createEmbeddedView calls [OK]
Quick Trick: Loop createEmbeddedView calls to repeat template N times [OK]
Common Mistakes:
  • Using Renderer2 to clone elements (not Angular way)
  • Trying to use *ngFor inside directive template
  • Creating multiple components unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes