{"id":2109,"date":"2024-07-11T17:39:30","date_gmt":"2024-07-12T00:39:30","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2109"},"modified":"2026-03-18T07:27:14","modified_gmt":"2026-03-18T14:27:14","slug":"let-for-local-variables-in-angular-views","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/let-for-local-variables-in-angular-views\/","title":{"rendered":"@let for local variables in Angular views"},"content":{"rendered":"\n<p>Angular 18.1 brought a new feature to our Angular templates: the ability to create local variables. Just like the new <a href=\"https:\/\/blog.angulartraining.com\/angular-17-new-control-flow-syntax-4fbec4772d04\" target=\"_blank\" rel=\"noopener\" title=\"\">control-flow blocks<\/a> <code>@if<\/code>, <code>@for<\/code>, and <code>@switch<\/code>, the new syntax uses the <code>@<\/code> character with a new keyword: <code>@let<\/code>.<\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"205\" height=\"85\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image.png\" alt=\"\" class=\"wp-image-2110\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code displays 21 as the value of the Angular expression. Why is this helpful? In many cases, we deal with complex nested objects that translate into long, verbose expressions:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"492\" height=\"75\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-1.png\" alt=\"\" class=\"wp-image-2111\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-1.png 492w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-1-300x46.png 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>With <code>@let<\/code>, we can make things shorter:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"119\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-2.png\" alt=\"\" class=\"wp-image-2112\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-2.png 499w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-2-300x72.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Another major use case is when using the async pipe. I covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/one-more-async-pipe-syntax-trick\/\" target=\"_blank\" rel=\"noopener\" title=\"\">several tricks on how to use the async pipe<\/a> in the past, even <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/ngrxlet-a-better-version-of-the-async-pipe\/\" target=\"_blank\" rel=\"noopener\" title=\"\">introducing <code>ngrxLet<\/code> as an alternative<\/a>, and all of these tricks can now be replaced with <code>@let<\/code>. <\/p>\n\n\n\n<p>Here is the initial problem with async when dealing with data from an Observable that we need in several places: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"110\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-3.png\" alt=\"\" class=\"wp-image-2113\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-3.png 636w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-3-300x52.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above syntax is not only horrible, but it also multiplies subscriptions to the source Observable, which is even worse. <\/p>\n\n\n\n<p> Here is what we can do with <code>@let<\/code> now:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"165\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-6.png\" alt=\"\" class=\"wp-image-2116\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-6.png 518w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-6-300x96.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>That&#8217;s a lot better. If the multiple <code>?<\/code> drive you crazy, you can do this instead:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"216\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-7.png\" alt=\"\" class=\"wp-image-2117\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-7.png 538w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/07\/image-7-300x120.png 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><code>@let<\/code> variables are read-only and cannot be reassigned. They behave like the <code>const<\/code> keyword in Javascript. The scope is also to the nearest block (which means <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/whats-an-angular-view\/\" target=\"_blank\" rel=\"noopener\" title=\"\">view<\/a>, in the case of Angular templates), so in my above example, <code>addr<\/code> is only available within the <code>@if<\/code> block, invisible outside of it.<\/p>\n\n\n\n<p>You can play with my <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-8dzj5h?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fmain.ts&amp;template=node&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">examples on Stackblitz here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular 18.1 brought a new feature to our Angular templates: the ability to create local variables. Just like the new control-flow blocks @if, @for, and @switch, the new syntax uses the @ character with a new keyword: @let. Here is an example: The above code displays 21 as the value of the Angular expression. Why [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,7],"tags":[],"class_list":["post-2109","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=2109"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2109\/revisions"}],"predecessor-version":[{"id":2397,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2109\/revisions\/2397"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}