python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
@Component vs @Directive

What Is a Template Reference variable?

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 -

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

// Reference cellnumber variable inside Component

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 a 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 a 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.

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions