Template Reference Variable using Select Box
<select #myColor (change) = "setData(myColor.value)"></select>
look at the code snippet, #myColor is a template reference variable. The selected value of select box can be accessed by myColor.value
Template Reference Variable with NgForm
how to access NgForm directive using template reference variable
<form (ngSubmit)="onSubmitPersonForm(myForm)" #myForm="ngForm">
<input name="name" required [(ngModel)]="person.pname">
<button type="submit" [disabled]="!myForm.form.valid">Submit</button>
</form>
ngSubmit: It enables binding angular expressions to onsubmit event of the form. Here on form submit onSubmitPersonForm component method will be called.
ngForm: It is the nestable alias of form directive
Here we are using template reference variable for ngForm as #myForm="ngForm" . Now we can use myForm in place of ngForm such as to check form validity and we can also use it in our angular
Template Reference Variable using Input Text Box
Template reference variable is a variable using which we can access DOM properties. In our example, we are using following DOM properties of the input box.
<input type="text" #mobile placeholder="Enter Mobile Number">
In the above input text box, #mobile is a template reference variable. To fetch DOM properties, we do as follows.
mobile.placeholder: It will give placeholder of our text box if we have specified.
mobile.value: It will give the value of our text box.
mobile.type: It will give the type of input element. In our example type is text.