Question presented to candidate: "What are @ViewChild and @ContentChild? Using a tabs component as the example, tell me what each one can see, when its value is available, and what the static option does."
What a strong answer should cover:
- Both are queries: a decorator on a property that Angular fills with a match from the template.
@ViewChildsearches the component's own template;@ContentChildsearches the projected content the parent placed between its tags.- Timing: dynamic results are set before
ngAfterViewInit/ngAfterContentInit;static: trueresolves beforengOnInitbut never updates, and is only valid for elements not inside@if/@for. - Locators: a template reference name (
'title'), a component or directive class, or a provider token;readpicks what to return (ElementRef,TemplateRef,ViewContainerRef, a directive). - A component template is a black box: content queries do not see inside other components' templates;
descendantscontrols nested matches. - The signal versions
viewChild()/contentChild()are the modern equivalent.
Clarifying questions expected:
- "Is the element always rendered, or inside an
@if?" - "Do you need the DOM element or the component instance?"
Code / implementation expected: Yes β a component that queries both its own template and its projected content and logs when each is available.