Question presented to candidate:
"You will see <ng-container> and <ng-template> all over Angular codebases. What is each one, what ends up in the DOM, and when would you reach for them?"
What a strong answer should cover:
ng-containeris a logical grouping element: its children render, but no element is created for it (Angular leaves only a comment anchor).ng-templateis a template definition: its content does not render by itself; it is aTemplateRefthat something must instantiate (ngTemplateOutlet, a structural directive,ViewContainerRef.createEmbeddedView).- Typical
ng-containeruses: hosting a directive without an extra element (for example*ngTemplateOutlet), grouping siblings where a wrapper would break HTML or CSS (inside<tbody>,<select>, flex/grid). - Typical
ng-templateuses: reusable fragments, templates passed to child components, the else branch of the old*ngIf, custom structural directives. - The
*prefix is sugar for wrapping an element in anng-template; only one*is allowed per element, which is whyng-containerwas used to stack them. - With built-in control flow (
@if,@for), many oldng-container/ng-templatewrappers are no longer needed.
Clarifying questions expected:
- "Are we on built-in control flow, or still on structural directives?"
Code / implementation expected: Yes β a template using both, ideally with ngTemplateOutlet.