@let in Angular 18: A Cleaner Way to Work with Variables

Code With Bilal
4 min readNov 15, 2024

--

Introduction

Angular 18 introduces the @let directive, an innovative feature that simplifies variable declarations directly within HTML templates. This tool enables developers to streamline their code by reducing the need for additional logic in TypeScript files or redundant structural directives in templates.

While still under development, @let has been officially released in Angular version 18.1.0, opening up new possibilities for cleaner, more efficient templates. Let’s dive into its usage, examples, and benefits.

What is @let?

The @let directive is designed to allow the creation of local variables directly in the HTML template. These variables can be used for operations like concatenating text, performing calculations, or processing asynchronous data, making template logic more concise and readable.

Why Use @let?

1. Cleaner Templates: Reduce repetitive or deeply nested ngIf statements.

2. Improved Readability: Variable declarations are more explicit and easier to follow.

3. Simplified Async Handling: Works seamlessly with observables and async pipes.

4. Enhanced Flexibility: Combine multiple conditions or values without creating intermediate components.

How to Use @let

Here’s a step-by-step guide on how to use @let in your Angular 18 projects

In this example, the variable greeting is defined in the HTML template, combining a string with the name variable. This reduces the need for TypeScript logic for simple operations.

Practical Usage Examples

1. Calculating Total Prices

If you have a list of products and want to calculate the total price for each, you can use @let like this:

This calculates the totalPrice directly in the template without cluttering the component code.

2. Formatting Dates

Formatting dates is a common task, and @let simplifies this process:

The formattedDate variable converts a raw date into a user-friendly format.

3. Conditional Messaging

You can use @let to dynamically generate messages based on conditions:

Here, statusMessage adapts to the user’s active status, improving clarity and readability.

4. Handling Asynchronous Data

One of the most significant advantages of @let is its ability to handle asynchronous data more elegantly:

With this, the observable data$ is processed directly in the template, avoiding the need for extra components or ngIf directives.

Advantages of @let

Cleaner Templates: Reduces the need for intermediate DOM elements or complex nested logic.

Simplified Async Handling: Works seamlessly with the | async pipe.

Improved Readability: Keeps logic close to the template elements it affects.

Reduced TypeScript Logic: Handles simple operations directly in HTML.

Limitations of @let

While @let is a powerful tool, it’s not always the best choice:

1. Complex Logic: For intricate operations, handling logic in TypeScript is more appropriate.

2. Reusability: If the same logic is needed across multiple components, functions in TypeScript are a better approach.

3. Performance: Using @let with extensive data processing directly in templates can lead to performance issues. Always test for efficiency.

Conclusion : The @let directive in Angular 18 is a game-changer for developers, offering a more intuitive and streamlined way to handle variables in templates. While it’s ideal for straightforward operations and async handling, developers should balance its use with traditional TypeScript logic for more complex scenarios.

As Angular continues to evolve, features like @let reinforce its position as a robust framework for building dynamic, efficient web applications. Start experimenting with @let in your projects to simplify template logic, improve readability, and reduce repetitive code. This addition empowers developers to write more concise and maintainable code, making Angular an even more powerful choice for modern web development.

“Follow me for in-depth Angular articles, from beginner tips to pro-level insights on the latest features!”

--

--

Code With Bilal
Code With Bilal

Written by Code With Bilal

Software Engineer | Angular | Problem Solver

Responses (2)