Member-only story
@let local variables in Angular views
Remil
Jul 21, 2024
@let in angular 18.1
The @let directive allows you to create variables directly in the HTML code
<div>
@let name= "remil";
{{name}}
</div>
In Angular, variables declared with @let are read-only and cannot be reassigned, similar to the const keyword. Additionally, their scope is limited to the nearest block, which in Angular templates, refers to the nearest view. For instance, in the example provided, the variable age is only accessible within the @if block and remains invisible outside of it.
<div>
@if(user){
@fullname =user.firstName +user.lastName;
<div>{{fullnmae}} </div>
<div>{{user.address}} </div>
}
</div>
it will help to do the calculation much easy way.
<div>
@for( product of products; track product.id){
@let discount =.10;
@let priceAfterDiscount = product.price*discount;
<div> {{ product.id}} -- {{priceAfterDiscount}}</div>
}
</div>