Question presented to candidate:
"Explain ElementRef, TemplateRef and ViewContainerRef. Then use them to write a tiny structural directive, something like *repeat="3", and tell me where the created views actually end up in the DOM."
What a strong answer should cover:
- ElementRef: a wrapper around a rendered element,
nativeElementis the real DOM node (a comment node forng-template); the typings flag it as a security risk to use directly. - TemplateRef: a blueprint of an
<ng-template>(or a*structural directive host) that is not rendered until someone stamps it out; injectable only where there is a template, otherwiseNG0201. - ViewContainerRef: an anchor point where views can be inserted;
createEmbeddedView(templateRef, context),createComponent(Type),clear(),length,move,indexOf. - Views are inserted as siblings next to the anchor (a comment node), not as children of it;
createComponenton a component's own container puts the new component after its host. - Template context:
$implicitand named values becomelet-variables (let n; let total = total). - How this relates to
ngTemplateOutlet,@if/@for, and dynamic components.
Clarifying questions expected:
- "Should the directive clean up and re-render when the count changes?"
- "Do you want to inject them or obtain them with a query?"
Code / implementation expected: Yes β a structural directive with TemplateRef + ViewContainerRef, plus a directive using ElementRef.