0
0
Angularframework~10 mins

Template reference variables in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Template reference variables
Template renders
Declare #ref on element
Access element or component via #ref
Use #ref in template expressions or event handlers
Angular updates view with #ref value
Angular reads the template, finds #ref variables, links them to elements or components, and allows access inside the template for interaction.
Execution Sample
Angular
<input #myInput type="text">
<button (click)="alert(myInput.value)">Show</button>
Defines a template reference variable #myInput on an input element and uses it in a button click to show the input's current value.
Execution Table
StepTemplate ElementActionTemplate Reference VariableValue at StepEffect
1<input>Create input element#myInputinput element referenceVariable #myInput points to input element
2<button>Create button elementnonenoneButton ready with click event
3Button click eventUser clicks button#myInputinput element referenceAccess input.value via #myInput
4Button click eventEvaluate alert(myInput.value)#myInputcurrent text in inputAlert shows input's current text
5EndNo more actionsnonenoneExecution stops
💡 User finishes interaction; no further template events
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
#myInputundefinedinput element referenceinput element referenceinput element referenceinput element reference
Key Moments - 2 Insights
Why can we use #myInput.value inside the button click event?
Because #myInput holds a reference to the input element, Angular lets us access its properties like 'value' directly in the template event, as shown in execution_table step 4.
Is #myInput available outside the template where it is declared?
No, #myInput is only accessible inside the template where it is declared, not in the component class or other templates, as seen in execution_table step 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does #myInput point to after step 1?
AThe input element reference
BThe button element reference
CUndefined
DThe component instance
💡 Hint
Check the 'Value at Step' column for step 1 in the execution_table
At which step does the button click event access #myInput.value?
AStep 3
BStep 4
CStep 1
DStep 2
💡 Hint
Look for 'Evaluate alert(myInput.value)' in the Action column of execution_table
If the input text changes before clicking the button, how does that affect #myInput.value in the alert?
AIt shows an empty string
BIt shows the original input text when the page loaded
CIt shows the updated input text
DIt causes an error
💡 Hint
Refer to variable_tracker showing #myInput always points to the input element, so its value reflects current text
Concept Snapshot
Template reference variables (#ref) let you name elements or components in Angular templates.
Use #ref on an element to access it elsewhere in the template.
Commonly used to read input values or call component methods.
Accessible only inside the template where declared.
Example: <input #myInput> then use myInput.value in events.
They provide direct access without needing component code.
Full Transcript
In Angular templates, you can create a template reference variable by adding # followed by a name on an element, like #myInput on an input field. This variable holds a reference to that element or component instance. You can then use this variable inside the template, for example in event handlers, to access properties or call methods. When the template renders, Angular links the variable to the actual element. When a user clicks a button that uses this variable, Angular accesses the current state of the element, such as the input's value, and performs the action like showing an alert. The variable only exists inside the template and is not accessible in the component class. This allows easy interaction with elements directly in the template without extra code.