Question presented to candidate:
"What does #box mean in an Angular template, what value does it hold in different places, and where can you use it?"
What a strong answer should cover:
#namedeclares a template reference variable: a name for something in the template, usable elsewhere in the same template.- What it refers to depends on where it sits: on a plain element it is the DOM element; on a component it is the component instance; on
<ng-template>it is aTemplateRef. #ref="exportName"picks a directive instance by itsexportAsname, for example#email="ngModel"or#f="ngForm".- Scope: visible anywhere in its template, but not outside an
@if/@for/<ng-template>block it was declared in, and never in the component class. - In the class you reach the same things with
viewChild()/@ViewChildqueries. - Good use: wiring two elements together without a class field. Poor use: building business logic in the template.
Clarifying questions expected:
- "Do you mean using it inside the template only, or getting hold of it in the class as well?"
Code / implementation expected: Yes β refs on an input, a component, a directive with exportAs, and ngModel.