Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

Template Reference Variables

Angular Template Reference Variables provide a convenient way to create a reference to an HTML element, component, or directive inside an Angular template.

What is a Template Reference Variable?

A template reference variable is a variable created directly inside an Angular template using the # symbol.

It provides a reference to an element or directive instance and allows other parts of the same template to access it.

Simple Definition

A Template Reference Variable gives a name to an element so that the template can access that element and its properties.

Template Reference Variable Flow

HTML Element Input / Button
→
Reference #studentInput
→
Template Access Value
→
UI Display Result

Template Reference Variable Syntax

Input Reference

Creates a reference to an input element.

#studentInput
Read Value

Reads the current value of an input.

studentInput.value
Use in Event

Passes the reference value to a method.

(click)="showName(studentInput.value)"
Display Data

Displays the referenced value in the template.

{{ studentInput.value }}

1. Creating a Template Reference Variable

A template reference variable is created by adding the # symbol followed by a variable name.

Example
<input #studentInput placeholder="Enter student name">
How it works

The #studentInput variable creates a reference to the input element. The template can then access properties of that input using studentInput.

2. Accessing Input Value

Once a reference variable is created, the template can access the current value of the referenced input element.

Template
<input #studentInput placeholder="Enter student name">

<p>
    Student Name:
    {{ studentInput.value }}
</p>
Expected Behavior:

If the user enters Sam:

Student Name: Sam

3. Using Reference Variable with Button

A template reference variable can be used inside an event statement to pass the current input value to a method.

Component
export class StudentComponent {

    message = '';

    showName(name: string) {
        this.message = 'Welcome ' + name;
    }

}
Template
<input #studentInput placeholder="Enter student name">

<button (click)="showName(studentInput.value)">
    Show Name
</button>

<p>
    {{ message }}
</p>
Expected Behavior:

Enter: Sam

After clicking the button: Welcome Sam

4. Multiple Template Reference Variables

Multiple elements in the same template can have their own reference variables.

Example
<input #studentName placeholder="Student Name">

<input #studentCourse placeholder="Course">

<button
    (click)="
        showStudent(
            studentName.value,
            studentCourse.value
        )
    ">
    Show Student
</button>
How it works

The template creates two references: #studentName and #studentCourse. Their values can be passed to a component method.

5. Template Reference Variable with Form

Template reference variables are also commonly used with forms and form controls.

Example
<form #studentForm="ngForm">

    <input
        name="studentName"
        ngModel
        placeholder="Enter student name">

    <button
        type="submit"
        [disabled]="studentForm.invalid">
        Submit
    </button>

</form>
Important

When a reference variable is assigned to a directive such as ngForm, the reference can provide access to the directive instance and its properties.

6. Referencing an Angular Component

A template reference variable can also be used to reference an Angular component instance.

Parent Template
<app-student #studentComponent>
</app-student>

<button (click)="studentComponent.showStudent()">
    Show Student
</button>
How it works

The #studentComponent reference points to the component instance, allowing the template to access its available public members.

Template Reference Variable vs Component Property

Feature Template Reference Variable Component Property
Created Where? Inside template Inside component class
Syntax #studentInput studentName = 'Sam'
Purpose Reference an element or directive Store application data
Access Template Component and template

Common Template Reference Examples

Input Element
<input #studentInput>
Read Value
studentInput.value
Button Event
(click)="show(studentInput.value)"
Form Reference
<form #studentForm="ngForm">

Complete Template Reference Variable Example

student.component.ts
export class StudentComponent {

    message = '';

    showStudent(name: string) {
        this.message = 'Welcome ' + name;
    }

}
student.component.html
<h2>
    Student Information
</h2>

<input
    #studentInput
    placeholder="Enter student name">

<button
    (click)="showStudent(studentInput.value)">
    Show Student
</button>

<p>
    {{ message }}
</p>
Expected Output:

Student Information

Enter: Sam

Welcome Sam

Template Reference Variable Best Practices

  • Use meaningful names for template reference variables.
  • Use reference variables when template-level access is required.
  • Avoid creating unnecessary references.
  • Use component properties for application-level data.
  • Keep template logic simple and readable.
  • Use reference variables carefully when working with forms and component instances.

Summary

Angular Template Reference Variables provide a convenient way to reference HTML elements, directives, forms, and component instances directly inside a template.

They are created using the # symbol and can be used to access values and call available functionality from the referenced object.

Template reference variables are especially useful for input elements, forms, event handling and component references.