AI Hints
Home›Angular Tutorial›@let, Class and Style Bindings and Practice: A Book List
Angular · Lesson 43 of 120

@let

Local names in templates.

⏱ 2 min read▶ Video at 51:37🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Sometimes a template works out the same value in several places. At let gives that value a name, right inside the template.

  • The same value, twice
  • Give it a name
  • @let

The @let declaration

At let, a name, equals, an expression, declares a local variable. You can use it anywhere below, in the same template. Its value updates when the signals it reads change. Keep the expressions short.

keyvalue
@let total = ...;declare a name
Useanywhere below it
Updateswith its signals
Keep itshort and simple

A total, used twice

Total is declared once, from price times copies. Free uses total, to check for free delivery. Then the paragraphs use both names. Run it. Two copies cost twenty one ninety eight. And the page shows how much more is needed for free delivery.

app.tstypescript
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    @let total = price() * copies();
    @let free = total >= 25;
    <h2>{{ copies() }} × Circe</h2>
    <p>Total: £{{ total.toFixed(2) }}</p>
    @if (free) { <p>Free delivery</p> }
    @else { <p>Add £{{ (25 - total).toFixed(2) }} for free delivery</p> }
  `,
})
export class App {
  price = signal(10.99);
  copies = signal(2);
}

@let or computed?

At let is handy for small values, used only in the template. Computed lives in the class, so the class, and tests, can use it too. If the logic grows, or matters to the business, move it into a computed signal.

@let

  • in the templatesmall, local valuesquick and simple

computed()

  • in the classreusable, testableimportant logic

Summary

At let names a value inside a template, and keeps it up to date. Use computed for bigger logic. Next, class, and style bindings.

Key takeaways
  • @let total = ...;
  • Used below
  • Updates itself
Next up: Class and Style Bindings — change the look