October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Variables in Angular Templates: `@let` and Template References

Angular’s @let declares an expression-backed local value; #name references a template object. Learn their syntax, scope, and key differences.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular templates support two kinds of variable declarations: @let for a local value derived from an expression, and #name for a reference to a template object such as an element, component, directive, or TemplateRef. Both are scoped to their template, so a nested block can use an outer variable, but a parent cannot access one declared only inside that block.

Choose the declaration that matches what you need

Form What it declares Typical use
@let name = expression; A local variable whose value comes from an Angular template expression and stays current as that expression changes. Reuse an expression value in several places in a template.
#name A reference to the object associated with the declaration site: usually an element, but potentially a component, directive, or TemplateRef. Refer to a template object, such as an input element or reusable template fragment.

Declare a local value with @let

Use @let when you want to give a template expression a name. The declaration has the form @let name = expression; and must end with a semicolon. Each declaration creates exactly one variable.

As an Amazon Associate I earn from qualifying purchases.

@let user = user$ | async;

@if (user) {
  <h1>Hello, {{ user.name }}</h1>
  <button (click)="update(user)">Update profile</button>
}

Angular keeps the variable’s value in sync with its expression. It is not a mutable JavaScript local: do not try to assign a new value to the variable. The expression can span multiple lines, but the declaration still ends with a semicolon.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use #name to reference a template object

Put a reference variable on the element or template you want to identify. What the variable refers to depends on that host:

  • On a regular element, it refers to the element instance.
  • On a component, it refers to the component instance.
  • On an <ng-template>, it refers to a TemplateRef.
  • On an element with a directive, it can refer to that directive’s instance if you specify a matching exportAs name.

Reference an element

<input #taskInput placeholder="Enter task name" />

Reference an exported directive

<section dropZone #firstZone="dropZone">...</section>

This form requires the directive to declare exportAs: 'dropZone'. A directive without an exportAs name cannot be referenced this way.

Use a reference with a view query

A reference can identify a target for a component view query, for example @ViewChild('description'). The reference’s value still depends on the host where it was declared.

Understand template scope

Template variables are available in their declaring template and nested scopes. A child scope can read a variable declared in its parent, but a parent cannot reach into a child scope. @let declarations are not hoisted, so declare a variable in the scope where it is needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@let topLevel = value;

@if (condition) {
  {{ topLevel }}
  @let nested = value;
  {{ nested }}
}

<!-- `nested` is unavailable here because it was declared in the @if scope. -->

The same rule applies to variables declared inside @for, structural directives, and <ng-template> scopes. A nested template can use an outer variable; the outer template cannot use a variable declared only inside the nested template.

Do not confuse @let with let- template parameters

@let creates a local variable from an expression. By contrast, a let- attribute on an <ng-template> declares a parameter that receives a value from that template fragment’s context object.

<ng-template #fragment let-topping="topping">
  <p>You selected: {{ topping }}</p>
</ng-template>

<ng-container
  [ngTemplateOutlet]="fragment"
  [ngTemplateOutletContext]="{ topping: 'onion' }" />

Here, topping is supplied through the outlet context. The fragment remains associated with the component in which it was declared.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Watch for name shadowing

Angular template expressions can access the component context as well as relevant template variables and locals. If a template variable has the same name as a component member, the template variable takes precedence. Use this.member when you mean the component property explicitly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Common mistakes

  • Trying to reassign a variable declared with @let.
  • Using a variable outside the template scope where it was declared.
  • Assuming every #name refers to a DOM element, regardless of its host.
  • Using #ref="name" without a directive that exports the matching name.
  • Treating an <ng-template> let- parameter as an @let declaration.
  • Forgetting that a local variable can shadow a component member.

Official Angular references

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.