{"id":337,"date":"2023-01-18T05:00:00","date_gmt":"2023-01-18T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=337"},"modified":"2023-01-18T17:09:04","modified_gmt":"2023-01-19T01:09:04","slug":"ngrxlet-a-better-version-of-the-async-pipe","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/ngrxlet-a-better-version-of-the-async-pipe\/","title":{"rendered":"ngrxLet: A better version of the async pipe"},"content":{"rendered":"\n<p>Yesterday, we mentioned <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/async-pipe-syntax-tricks\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how the <code>async<\/code> pipe can be used with <code>*ngIf<\/code> or <code>*ngFor<\/code><\/a> in our templates to declare a local variable that allows us to have multiple expressions using the same data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> *<span class=\"hljs-attr\">ngIf<\/span>=<span class=\"hljs-string\">\"user$ | async as user\"<\/span>&gt;<\/span>\n   <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>First name: {{ user.firstName }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n   <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Last name: {{ user.lastName }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>While the above code works perfectly well, it&#8217;s not always convenient to add an additional <code>*ngIf<\/code> or <code>*ngFor<\/code> in our templates. Another shortcoming of the async pipe is that it doesn&#8217;t let us know if the <code>observable<\/code> has an error or completes successfully.<\/p>\n\n\n\n<p>This is where the <a href=\"https:\/\/ngrx.io\/guide\/component\/let\" target=\"_blank\" rel=\"noopener\" title=\"\">ngrxLet directive<\/a> can save the day, as it solves all of the above shortcomings with a straightforward syntax. Our previous example becomes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> *<span class=\"hljs-attr\">ngrxLet<\/span>=<span class=\"hljs-string\">\"user$ as user\"<\/span>&gt;<\/span>\n   <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>First name: {{ user.firstName }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n   <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Last name: {{ user.lastName }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And if we want to get any errors or the completion status of the observable, we can do so with more local variables exposed by <code>ngrxLet<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> *<span class=\"hljs-attr\">ngrxLet<\/span>=<span class=\"hljs-string\">\"user$ as user; error as e; complete as c\"<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can find a <a href=\"https:\/\/stackblitz.com\/edit\/ngrx-component?file=src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">complete working example here<\/a>. <code>ngrxLet<\/code> can be installed as a dependency using npm (<code>npm install @ngrx\/component<\/code>), and it&#8217;s important to note that it is not the entire <code>ngrx<\/code> state management library, just a tiny subset of it, so using that directive does not require using anything else from <code>ngrx<\/code>.<\/p>\n\n\n\n<p>Here is a link to a slightly expanded version of that tutorial with more information if you want to dig deeper into it: <a href=\"https:\/\/blog.angulartraining.com\/ngrxlet-a-better-version-of-the-async-pipe-37bfc5c7a5ec\" target=\"_blank\" rel=\"noopener\" title=\"\">ngrxLet &#8211; A better version of the async pipe<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we mentioned how the async pipe can be used with *ngIf or *ngFor in our templates to declare a local variable that allows us to have multiple expressions using the same data: While the above code works perfectly well, it&#8217;s not always convenient to add an additional *ngIf or *ngFor in our templates. Another [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,10,7],"tags":[],"class_list":["post-337","post","type-post","status-publish","format-standard","hentry","category-ngrx","category-rxjs","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/337","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=337"}],"version-history":[{"count":7,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/337\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/337\/revisions\/357"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}