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.
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:
#1 Best Overall
- 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 aTemplateRef. - On an element with a directive, it can refer to that directive’s instance if you specify a matching
exportAsname.
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.
Rank #2
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.
Recommended Free Tools
@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.
Rank #3
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.
Rank #4
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.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchQuick Recap
Common mistakes
- Trying to reassign a variable declared with
@let. - Using a variable outside the template scope where it was declared.
- Assuming every
#namerefers 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@letdeclaration. - 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.




