{"id":374,"date":"2023-01-26T05:00:00","date_gmt":"2023-01-26T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=374"},"modified":"2023-01-24T14:45:01","modified_gmt":"2023-01-24T22:45:01","slug":"rxjs-withlatestfrom-operator","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-withlatestfrom-operator\/","title":{"rendered":"RxJs withLatestFrom operator"},"content":{"rendered":"\n<p>Our <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/category\/rxjs\/\" target=\"_blank\" rel=\"noopener\" title=\"\">weekly RxJs operator<\/a> is <code>withLatestFrom<\/code>, a rare example of an operator with a name that says it all.<\/p>\n\n\n\n<p>Here&#8217;s the <a href=\"https:\/\/rxmarbles.com\/#withLatestFrom\" target=\"_blank\" rel=\"noopener\" title=\"\">marble diagram<\/a> for <code>withLatestFrom<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"413\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-7.png\" alt=\"\" class=\"wp-image-375\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-7.png 1000w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-7-300x124.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-7-768x317.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>This operator takes <strong>two or more Observables<\/strong> and turns them into <strong>one single Observable<\/strong> that emits a new value whenever the<strong> source Observable emits something new<\/strong>.<\/p>\n\n\n\n<p>The important thing to note here is that only one Observable triggers updates in the output Observable. As a result, <code>withLatestFrom<\/code> is a great option when you want a user action to trigger an update in the user interface (think about data table filters, inter-connected dropdowns, etc.)<\/p>\n\n\n\n<p>You can see a live <a href=\"https:\/\/stackblitz.com\/edit\/at-rxjs-demo4?file=src%2Fapp%2Fapp.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">code example here<\/a> (including comments that explain the thought process) with two dropdowns that depend on one another. A selection in the first dropdown impacts the values displayed in the second one (selecting a continent filters out the countries shown in the second dropdown:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"333\" height=\"148\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-8.png\" alt=\"\" class=\"wp-image-376\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-8.png 333w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/01\/image-8-300x133.png 300w\" sizes=\"auto, (max-width: 333px) 100vw, 333px\" \/><\/figure>\n\n\n\n<p>If you want a deeper dive, I have this <a href=\"https:\/\/blog.angulartraining.com\/dynamic-filtering-with-rxjs-and-angular-forms-a-tutorial-6daa3c44076a\" target=\"_blank\" rel=\"noopener\" title=\"\">complete tutorial on using <code>withLatestFrom<\/code> alongside other operators to implement dynamic filtering in a component<\/a>.<\/p>\n\n\n\n<p>And for even more RxJs content, my 2-hour <a href=\"https:\/\/www.youtube.com\/watch?v=nQrMgNIvqxM\" target=\"_blank\" rel=\"noopener\" title=\"\">RxJS workshop from ng-conf 2022<\/a> is now available on Youtube for free!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our weekly RxJs operator is withLatestFrom, a rare example of an operator with a name that says it all. Here&#8217;s the marble diagram for withLatestFrom: This operator takes two or more Observables and turns them into one single Observable that emits a new value whenever the source Observable emits something new. The important thing to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-operators","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/374","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=374"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/374\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}