Template Local Variables with @let in Angular

Template Local Variables with @let in Angular

Angular's powerful templating system allows developers to create dynamic and interactive user interfaces with ease. One of the features that can significantly enhance your templates is the use of template local variables with the @let directive. In this article, we will explore what template local variables are, how to use the @let directive, and practical examples to help you get started.

Understanding Template Local Variables

Template local variables in Angular provide a way to reference DOM elements and component instances within your template. They allow you to access and manipulate various elements and data directly in your template without needing to bind everything to your component class.

Creating Template Local Variables

You can create a template local variable by using the # symbol followed by the variable name. For example, to reference an input element, you can do the following:

<input #myInput type="text" />
<button (click)="logInputValue(myInput.value)">Log Input Value</button>

In this example, #myInput is a template local variable that references the input element. The button's click event handler can access the input's value using myInput.value.

Introducing the @let Directive

Angular introduced the @let directive to enhance the way we declare and use template local variables. The @let directive allows you to define local variables within structural directives like *ngIf and *ngFor, making your templates cleaner and more readable.

Using @let with *ngIf

The *ngIf directive conditionally includes or excludes an element from the DOM based on a boolean expression. With the @let directive, you can create local variables to hold the result of the expression, which can be used within the template.

<div *ngIf="user as loggedInUser; else loginPrompt">
<p>Welcome, {{ loggedInUser.name }}!</p>
</div>
<ng-template #loginPrompt>
  <p>Please log in to continue.</p>
</ng-template>

In this example, user as loggedInUser creates a local variable loggedInUser that holds the value of user if the condition is true. This makes the template more readable and allows you to avoid repeating the user expression.

Using @let with *ngFor

The *ngFor directive is used to repeat a portion of the template for each item in a collection. With the @let directive, you can create local variables to hold each item and even index or other properties.

<ul>
    <li *ngFor="let item of items; let i = index">
    {{ i + 1 }}. {{ item.name }}
</li>
</ul>

In this example, let i = index creates a local variable i that holds the current index of the iteration. This can be useful for displaying item numbers or performing other operations based on the index.

Practical Examples of @let Usage

Let's look at some practical examples to see how the @let directive can be used to simplify and enhance your Angular templates.

Example 1: Displaying User Information

Suppose you have a component that fetches user information from a service and you want to display it conditionally.

<div *ngIf="user$ | async as user; else loading">
  <p>Name: {{ user.name }}</p>
  <p>Email: {{ user.email }}</p>
</div>
<ng-template #loading>
  <p>Loading user information...</p>
</ng-template>

In this example, user$ | async as user creates a local variable user that holds the result of the async pipe. This allows you to access user directly within the template.

Example 2: Iterating Over a List of Products

Suppose you have a list of products and you want to display them in a table with the product name and price.

<table>
    <tr *ngFor="let product of products">
    <td>{{ product.name }}</td>
    <td>{{ product.price | currency }}</td>
</tr>
</table>

In this example, let product of products creates a local variable product for each item in the products array, making it easy to access each product's properties.

Conclusion

The @let directive in Angular provides a powerful way to declare and use template local variables within structural directives. By using @let, you can create cleaner, more readable templates and avoid redundancy. Whether you're working with *ngIf, *ngFor, or other structural directives, incorporating @let can enhance your development workflow and improve the maintainability of your code.

Stay tuned for more Angular tips and best practices in our upcoming posts!

Thanks for Reading!

If you enjoyed this article and found it useful, and you'd like to explore more on coding, feel free to support me by buying me a coffee ☕️ or by following me on Instagram @bilalelmahdaoui and X (formerly Twitter) @bilalelmahdaoui. I regularly post updates, insights, videos, and podcasts, all focused on the latest in Angular. Stay tuned and don’t miss out on what’s happening! ✨