|
| 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. |
0 commit comments