Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Angular Template Reference Variable

What Is a Template Reference Variable?
A Template Reference Variable is a way of capturing a reference to a specific element, component, directive, and pipe so that it can be used someplace in the same template HTML.

You should declare a Reference Variable using the hash symbol (#).

The Angular componentsand directives only match selectors for classes that are declared in the Angular module.
Template Reference Variable Syntax
You can use a template reference variable by two ways.
1.      Using hash symbol (#)
2.      Using reference symbol (ref-)

The following examples of specifying a template reference variable using Input Text Box
I have declared a reference variable “cellnumber” using the hash symbol (#) and reference symbol (ref-).

inputtype="text"ref-cellnumber> //cellnumber will be a template reference variable.

input#cellnumber placeholder="Cell number"> //cellnumber will be a template reference variable.

I have created a reference to the input element that can be used later on in my template and the scope for “cellnumber” variable is the entire HTML template in which the reference is defined.

Here is how I could use that reference to get the value of the input for instance –
//cellnumber refers to the input element
button(click)="show(cellnumber)">click to seebutton>

In the below line of code, the variable “cellnumber” refer to the HTMLElement object instance for the input -

show(cellnumber: HTMLInputElement){

You can use the ViewChild decorator to reference it inside your component.
import{ViewChild, ElementRef} from '@angular/core';

// Reference cellnumber variable inside Component
@ViewChild('cellnumber') cellInputRef: ElementRef;

And finally, you can use this.nameInputRef anywhere inside your component class.
  this.contactNumber= this.cellInputRef.nativeElement.value

Template Reference Variable with NgForm

Here we will discuss about how to access NgForm directive using template reference variable.
  label>F-Name label>inputname="f-name"required [(ngModel)]="employee.fname">
  label>L-Name label>inputname="l-name"required [(ngModel)]="employee.lname">
  label>Age    label>inputname="age"required [(ngModel)]="employee.age">

In the above NgForm example contains an ngSubmit event and form directive.

The ngSubmit – The ngSubmit directive specifies a function to run when the form is submitted. Here on form submit onSubmitEmployee component method will be called.

The NgForm - It is nestable alias of form directive. The main purpose of ngForm is to group the controls, but not a replacement of

As you know, the HTML does not allow nesting of form elements. It is very useful to nest forms.

I hope you enjoyed this post. So please write your thoughts in the below comment box. Thank you so much for reading this post.

Share the post

Angular Template Reference Variable


Subscribe to Angular 2 Javascript C# Sql Server Knockoutjs Angularjs Kendo Ui Html 5 Web Api Wpf Wcf Etc.

Get updates delivered right to your inbox!

Thank you for your subscription