Angular template reference variable (#var)

Posted by q1234ask on Mon, 13 Jul 2020 16:35:15 +0200

1. Template Reference Variables

Template reference variables use the pound sign (#) to declare reference variables.

Template reference variables are often used to reference a DOM element in a template, which can reference Angular components or directives or Web Component s.

We can use a template to reference variables anywhere in the current template.


  <div class="panel panel-primary">
    <div class="panel-heading">
      <div class="panel-title">Template Reference Variable 1</div>
    <div class="panel-body">
      <input type="text" name="phone" placeholder="Please enter your mobile number" class="form-control" #phone (blur)="showOne(phone.value);">
      <span class="lable lable-primary">{{phone.value}}</span>

ts method definition:

  showOne(str: string) {;

2. NgForm Directive

Template reference variables are commonly used when defining a Form form.

Note: Parsing forms in Angular requires importing FormsModule into the current module.

In this way, the reference to the Angular NgForm directive has the ability to track the values and validity of each space in the form.


import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from "@angular/forms";

import { AppComponent } from './app.component';
import { StudentComponent } from "./student/student.component";

  declarations: [
  imports: [
  providers: [],
  bootstrap: [
   // AppComponent,
export class AppModule { }

An example of a complete form submission is as follows:

Html template:

  <div class="panel panel-warning">
    <div class="panel-heading">
      <div class="panel-title">Template Reference Variable 2-form</div>
    <div class="panel-body">
      <form (ngSubmit)="onSubmit(stuForm)" #stuForm="ngForm">
        <div class="form-group">
          <label for="name">Full name:</label>
          <input type="text" name="name" required [(ngModel)]="" class="form-control">

        <div class="form-group">
          <label for="age">Age:</label>
          <input type="number" name="age" required [(ngModel)]="stu.age" class="form-control">
        <button *ngIf="!issubmit" class="btn btn-success" type="submit" [disabled]="!stuForm.form.valid">Determine Submission</button>
        <button *ngIf='issubmit' type="submit" disabled class="btn btn-success">Submitting...</button>

      <div class="alert alert-info">

        form object:{{stu|json}}
Component definition in ts:

export class StudentComponent implements OnInit {

  constructor() { }
  ngOnInit() {

  stu = {};//Empty Object
  public issubmit: boolean = false;

  showOne(str: string) {;

  onSubmit(model: NgForm) {;

    //Because read-only cannot set the disabled property of the action button
    this.issubmit = true;
    setTimeout(() => {
      this.issubmit = false;
    }, 1000);
Tracking NgForm objects:

Submit action process:


Angular pipe operator (|)

Angular secure navigation operator (?.) and empty attribute path

VS Code for Angular2 Development Environment

Topics: angular Mobile JSON Attribute