How to use template reference variables in angular
Template reference variables allow us to transfer data from one part of a template to another or across different templates. They are useful when we need to handle user input or form data. They let us access parts of our templates, whether that be an element, component, or directive. Let’s review the four basic ways of using template reference variables in our Angular application.
Syntax
The simplest way to refer to any part of our Angular template is by using a template reference variable name prepended with a # symbol. This way, we can use the referred element anywhere inside our template.
DOM element
The following example highlights how to reference any DOM element, for example, an HTML input element.
app.component.html: Look at lines 14 and 15, the#educative_user_input1template reference variable refers to theinputtag. We use this to pass the value of theinputelement to thealert_educative_userfunction. This function is called when the submit button is clicked.app.component.ts: Thealert_educative_userupdates theeducative_user_inputvariable. This function will display the user’s input in an alert box.
The ng-template element
Template references can also be used with ng-templates. These
app.component.html: The lines 13 to 15 utilize ang-templateto enclose anh1element. We refer to theng-templateusing the template reference variableeducative_ng_template.app.component.ts: UseTemplateRefto access theng-templateinside the app component and theViewChilddecorator to inject theeducative_ng_templateas an instance ofTemplateRefclass inside the app component. WhileTemplateRefcontains the reference to the template defined inng-template,ViewContainerRefrefers to the element that hosts the component. ThecreateEmbeddedViewadds the template inside the component.
Component
Like a DOM element or ng-template, we can reference a component. This is particularly useful for accessing child components’ public attributes and methods inside parent components.
Use the following syntax to refer to any child component’s public attributes or functions within the parent.
app.component.html: The<app-educative-component #educatives_child_component></app-educative-component>will render the child’s content inside the app component. The#educatives_child_componentis a template reference variable that refers to the entire child component.educative-component.component.ts: We declare achilds_test_stringstring that can be accessed inside our app component via the template reference variable.
Directive
Angular directives add or remove elements from the DOM. They are extensions of HTML elements. Thus, like any other HTML element, they can be referenced with template reference variables.
app.component.html: Thehidden-divcustom directive is applied to the div element. We use the template reference variable#educatives_template_reference_variableto refer to this directive.app.module.ts: To utilize the newly created directive in the codebase, ensure that it is included underdeclarationsso that it is recognized as part of the app module.hidden-div.directive.ts: This file creates an Angular directive. We’ll apply this directive with the namehidden-divon any element inside our app component. We’ve saved the reference of the element on which the directive will be applied on usingElementRef. The reference of the element upon which the directive is applied will be saved inside theeducatives_directivevariable. Upon running the example above we’ll see that a yellow highlight will appear on top of the referenced element because of this.
Conclusion
In conclusion, template reference variables provide a shorter alternative to access DOM elements, components, and directives, unlike other referencing mechanisms like event listeners.
Free Resources