Angular 5 Template Reference variable

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 components and 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-).

<input type="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 see</button>

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.
<form (ngSubmit)="onSubmitEmployee(empForm)" #empForm="ngForm">
  <label>F-Name </label><input name="f-name" required [(ngModel)]="employee.fname">
  <label>L-Name </label><input name="l-name" required [(ngModel)]="employee.lname">
  <label>Age    </label><input name="age" required [(ngModel)]="employee.age">
  <button type="submit" [disabled]="!empForm.form.valid">Submit</button>

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 <form> tag.

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.

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2

Angular Template Reference Variable Angular Template Reference Variable Reviewed by Anil Singh on 1:23 AM Rating: (5) Powered by Blogger.