Skip to content

Commit 02a31c5

Browse files
crisbetopkozlowski-opensource
authored andcommitted
docs: add documentation page for let declarations (angular#56886)
Adds a docs page for the new `@let` syntax in Angular 18.1. PR Close angular#56886
1 parent 5f8cf02 commit 02a31c5

File tree

3 files changed

+125
-14
lines changed

3 files changed

+125
-14
lines changed

adev/src/app/sub-navigation-data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
228228
path: 'guide/templates/control-flow',
229229
contentPath: 'guide/templates/control-flow',
230230
},
231+
{
232+
label: 'Local template variables with @let',
233+
path: 'guide/templates/let-template-variables',
234+
contentPath: 'guide/templates/let-template-variables',
235+
},
231236
{
232237
label: 'Pipes',
233238
children: [
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Local template variables
2+
3+
Angular's `@let` syntax allows you to define a local variable and re-use it across the template.
4+
5+
IMPORTANT: the `@let` syntax is currently in [Developer Preview](/reference/releases#developer-preview).
6+
7+
## Syntax
8+
9+
`@let` declarations are similar to [JavaScript's `let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) and
10+
their values can be any valid Angular expression. The expressions will be re-evaluated any time the
11+
template is executed.
12+
13+
```html
14+
@let name = user.name;
15+
@let greeting = 'Hello, ' + name;
16+
@let data = data$ | async;
17+
@let pi = 3.1459;
18+
@let coordinates = {x: 50, y: 100};
19+
@let longExpression = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit ' +
20+
'sed do eiusmod tempor incididunt ut labore et dolore magna ' +
21+
'Ut enim ad minim veniam...';
22+
```
23+
24+
### Referencing the value of `@let`
25+
26+
Once you've declared the `@let`, you can reuse it anywhere in the template:
27+
28+
```html
29+
@let user = user$ | async;
30+
31+
@if (user) {
32+
<h1>Hello, {{user.name}}</h1>
33+
<user-avatar [photo]="user.photo"/>
34+
35+
<ul>
36+
@for (snack of user.favoriteSnacks; track snack.id) {
37+
<li>{{snack.name}}</li>
38+
}
39+
</ul>
40+
41+
<button (click)="update(user)">Update profile</button>
42+
}
43+
```
44+
45+
## Assignability
46+
47+
A key difference between `@let` and JavaScript's `let` is that `@let` cannot be re-assigned
48+
within the template, however its value will be recomputed when Angular runs change detection.
49+
50+
```html
51+
@let value = 1;
52+
53+
<!-- Invalid -->
54+
<button (click)="value = value + 1">Increment the value</button>
55+
```
56+
57+
## Scope
58+
59+
`@let` declarations are scoped to the current view and its descendants. Since they are not
60+
hoisted, they **cannot** be accessed by parent views or siblings:
61+
62+
```html
63+
@let topLevel = value;
64+
65+
<div>
66+
@let insideDiv = value;
67+
</div>
68+
69+
{{topLevel}} <!-- Valid -->
70+
{{insideDiv}} <!-- Valid -->
71+
72+
@if (condition) {
73+
{{topLevel + insideDiv}} <!-- Valid -->
74+
75+
@let nested = value;
76+
77+
@if (condition) {
78+
{{topLevel + insideDiv + nested}} <!-- Valid -->
79+
}
80+
}
81+
82+
<div *ngIf="condition">
83+
{{topLevel + insideDiv}} <!-- Valid -->
84+
85+
@let nestedNgIf = value;
86+
87+
<div *ngIf="condition">
88+
{{topLevel + insideDiv + nestedNgIf}} <!-- Valid -->
89+
</div>
90+
</div>
91+
92+
{{nested}} <!-- Error, not hoisted from @if -->
93+
{{nestedNgIf}} <!-- Error, not hoisted from *ngIf -->
94+
```
95+
96+
## Syntax definition
97+
98+
The `@let` syntax is formally defined as:
99+
* The `@let` keyword.
100+
* Followed by one or more whitespaces, not including new lines.
101+
* Followed by a valid JavaScript name and zero or more whitespaces.
102+
* Followed by the = symbol and zero or more whitespaces.
103+
* Followed by an Angular expression which can be multi-line.
104+
* Terminated by the `;` symbol.

adev/src/content/guide/templates/overview.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ For more information, see the [Security](best-practices/security) page.
3232

3333
You might also be interested in the following:
3434

35-
| Topics | Details |
36-
| :------------------------------------------------------------------------ | :-------------------------------------------------------------------- |
37-
| [Interpolation](guide/templates/interpolation) | Learn how to use interpolation and expressions in HTML. |
38-
| [Template statements](guide/templates/template-statements) | Respond to events in your templates. |
39-
| [Binding syntax](guide/templates/binding) | Use binding to coordinate values in your application. |
40-
| [Property binding](guide/templates/property-binding) | Set properties of target elements or directive `@Input()` decorators. |
41-
| [Attribute, class, and style bindings](guide/templates/attribute-binding) | Set the value of attributes, classes, and styles. |
42-
| [Event binding](guide/templates/event-binding) | Listen for events and your HTML. |
43-
| [Two-way binding](guide/templates/two-way-binding) | Share data between a class and its template. |
44-
| [Built-in directives](guide/directives) | Listen to and modify the behavior and layout of HTML. |
45-
| [Template reference variables](guide/templates/reference-variables) | Use special variables to reference a DOM element within a template. |
46-
| [Inputs](guide/components/inputs) | Accepting data with input properties |
47-
| [Outputs](guide/components/outputs) | Custom events with outputs |
48-
| [SVG in templates](guide/templates/svg-in-templates) | Dynamically generate interactive graphics. |
35+
| Topics | Details |
36+
| :------------------------------------------------------------------------ | :---------------------------------------------------------------------------- |
37+
| [Interpolation](guide/templates/interpolation) | Learn how to use interpolation and expressions in HTML. |
38+
| [Template statements](guide/templates/template-statements) | Respond to events in your templates. |
39+
| [Binding syntax](guide/templates/binding) | Use binding to coordinate values in your application. |
40+
| [Property binding](guide/templates/property-binding) | Set properties of target elements or directive `@Input()` decorators. |
41+
| [Attribute, class, and style bindings](guide/templates/attribute-binding) | Set the value of attributes, classes, and styles. |
42+
| [Event binding](guide/templates/event-binding) | Listen for events and your HTML. |
43+
| [Two-way binding](guide/templates/two-way-binding) | Share data between a class and its template. |
44+
| [Control flow](guide/templates/control-flow) | Angular's syntax for conditionally showing, hiding, and repeating elements. |
45+
| [Local template variables](let-template-variables) | Define and reuse variables in your template. |
46+
| [Built-in directives](guide/directives) | Listen to and modify the behavior and layout of HTML. |
47+
| [Template reference variables](guide/templates/reference-variables) | Use special variables to reference a DOM element within a template. |
48+
| [Inputs](guide/components/inputs) | Accepting data with input properties |
49+
| [Outputs](guide/components/outputs) | Custom events with outputs |
50+
| [SVG in templates](guide/templates/svg-in-templates) | Dynamically generate interactive graphics. |

0 commit comments

Comments
 (0)