0
0
Angularframework~10 mins

Template reference for direct access in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Template reference for direct access
Template renders
Element with #ref created
Component accesses element via #ref
Use element properties or methods
Update view or trigger actions
The template creates a reference to an element using #ref. The component accesses this element directly to read or change it.
Execution Sample
Angular
<input #myInput type="text" />
<button (click)="logValue(myInput.value)">Log</button>

logValue(value: string) {
  console.log(value);
}
This code uses a template reference #myInput to get the input's value when the button is clicked and logs it.
Execution Table
StepTemplate ActionReference CreatedComponent AccessOutput/Effect
1Render <input> with #myInputmyInput points to input elementNot accessed yetInput box appears on screen
2Render <button> with click eventNo new referenceNot accessed yetButton appears on screen
3User types 'Hello' in inputmyInput.value = 'Hello'Not accessed yetInput shows 'Hello'
4User clicks buttonReference still validlogValue called with 'Hello'Console logs: Hello
5User changes input to 'World'myInput.value = 'World'Not accessed yetInput shows 'World'
6User clicks button againReference still validlogValue called with 'World'Console logs: World
7End of interactionReference remainsNo further accessNo change
💡 User stops interacting; template reference remains valid throughout component lifecycle
Variable Tracker
VariableStartAfter Step 3After Step 5Final
myInput.value'' (empty)'Hello''World''World'
logValue inputN/A'Hello''World'N/A
Key Moments - 3 Insights
Why can the component access the input value directly using #myInput?
Because #myInput creates a direct reference to the input element in the template, allowing the component to read its properties like 'value' as shown in steps 3 and 4.
Does the template reference #myInput change when the user types?
No, the reference stays the same. Only the property 'value' of the referenced element changes, as seen in steps 3 and 5.
What happens if the button is clicked before typing anything?
The logValue function is called with an empty string because the input's value is initially empty, similar to step 1's initial state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of myInput.value at Step 5?
A'World'
B'Hello'
C'' (empty)
Dundefined
💡 Hint
Check the 'Reference Created' column at Step 5 in the execution table
At which step does the component first access the input's value?
AStep 3
BStep 4
CStep 1
DStep 6
💡 Hint
Look for 'Component Access' column where logValue is called
If the user never types anything, what will be logged when the button is clicked?
A'Hello'
B'World'
C'' (empty string)
Dnull
💡 Hint
Refer to the initial value of myInput.value in variable_tracker and execution_table Step 1
Concept Snapshot
Template reference (#ref) lets you grab an element in the template.
Use it to read or change element properties directly.
Example: <input #myInput> then access myInput.value.
Works only inside the template or event handlers.
Great for quick access without extra code.
Full Transcript
In Angular, you can create a template reference by adding #name to an element. This reference points directly to that element. For example, #myInput on an input box lets you access its value property directly. When the user types, the input's value changes but the reference stays the same. When a button is clicked, the component can use the reference to get the current input value and do something with it, like logging it. This method is simple and avoids extra code for accessing elements. The reference remains valid as long as the element is in the template.