0
0
Angularframework~5 mins

Template reference for direct access in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a template reference variable in Angular?
A template reference variable is a way to access a DOM element or Angular component directly within the template using a special syntax with the # symbol.
Click to reveal answer
beginner
How do you declare a template reference variable for an input element?
You declare it by adding #variableName inside the input tag, for example: <input #myInput>. This lets you access the input element directly in the template.
Click to reveal answer
intermediate
How can you use a template reference variable to get the value of an input field in Angular?
You can use the variable inside the template, for example: <button (click)="doSomething(myInput.value)">. This passes the current value of the input to the component method.
Click to reveal answer
intermediate
Can template reference variables access Angular components as well as DOM elements?
Yes, if you assign a template reference variable to a component selector, you can access the component instance and call its methods or access its properties directly in the template.
Click to reveal answer
beginner
What is the scope of a template reference variable in Angular templates?
The scope of a template reference variable is limited to the template where it is declared. It cannot be accessed in other templates or component classes directly.
Click to reveal answer
How do you declare a template reference variable named 'inputBox' on an input element?
A<input #inputBox>
B<input ref='inputBox'>
C<input var='inputBox'>
D<input $inputBox>
What can a template reference variable NOT directly access?
AComponent class properties
BAngular components
CDOM elements
DTemplate elements
Which of these is a valid use of a template reference variable in a button click event?
A<button (click)="submit(#inputRef.value)">
B<button (click)="submit(inputRef.value)">
C<button (click)="submit($inputRef.value)">
D<button (click)="submit(ref.inputRef.value)">
If you declare , what does compRef represent?
AThe DOM element of my-comp
BA CSS class applied to my-comp
CA string with the component name
DThe Angular component instance of my-comp
Where can you use a template reference variable declared in a template?
AInside the component class
BAnywhere in the Angular app
COnly inside the same template
DIn sibling components
Explain how to create and use a template reference variable to get the value of an input field in Angular.
Think about how you can grab the input's current text without using component code.
You got /3 concepts.
    Describe the difference between accessing a DOM element and an Angular component using template reference variables.
    Consider what you get when you put #var on a normal HTML tag vs a component tag.
    You got /3 concepts.