<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raj Saha</title>
    <description>The latest articles on DEV Community by Raj Saha (@rajsaha).</description>
    <link>https://dev.to/rajsaha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F36901%2Fd3ba1672-412b-4393-bcca-88c18852c312.jpeg</url>
      <title>DEV Community: Raj Saha</title>
      <link>https://dev.to/rajsaha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajsaha"/>
    <language>en</language>
    <item>
      <title>Listening to multiple form controls in Angular</title>
      <dc:creator>Raj Saha</dc:creator>
      <pubDate>Sat, 06 Aug 2022 12:16:29 +0000</pubDate>
      <link>https://dev.to/rajsaha/listening-to-multiple-form-controls-in-angular-im3</link>
      <guid>https://dev.to/rajsaha/listening-to-multiple-form-controls-in-angular-im3</guid>
      <description>&lt;p&gt;Let's say you're in a situation where you're listening to one &lt;code&gt;FormControl&lt;/code&gt; but you're dependent on the value of another &lt;code&gt;FormControl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are two elegant (in my opinion) solutions to this that I've come across.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution #1 - use &lt;code&gt;withLatestFrom&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://rxjs.dev/api/operators/withLatestFrom" rel="noopener noreferrer"&gt;&lt;code&gt;withLatestFrom&lt;/code&gt;&lt;/a&gt; is an rxjs operator that, as the name suggests, lets you pull the latest value of whatever Observable(s) that you pipe in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dab42wjlv2yzjwjwcx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dab42wjlv2yzjwjwcx5.png" alt="Image that shows the following code block  raw `&amp;lt;br&amp;gt;
this.form&amp;lt;br&amp;gt;
    .get('field2')&amp;lt;br&amp;gt;
    ?.valueChanges.pipe(&amp;lt;br&amp;gt;
        withLatestFrom(this.form.controls['field1']?.valueChanges)&amp;lt;br&amp;gt;
    )&amp;lt;br&amp;gt;
    .subscribe({&amp;lt;br&amp;gt;
        next: ([field1, field2]) =&amp;gt; {&amp;lt;br&amp;gt;
            // Do something with field1 and field2&amp;lt;br&amp;gt;
        },&amp;lt;br&amp;gt;
    });&amp;lt;br&amp;gt;
` endraw "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I am listening to the stream of &lt;code&gt;field2&lt;/code&gt; but the subscription is executed only when &lt;code&gt;field1&lt;/code&gt; has a value.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution #2 - use &lt;code&gt;combineLatestWith&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://rxjs.dev/api/operators/combineLatestWith" rel="noopener noreferrer"&gt;&lt;code&gt;combineLatestWith&lt;/code&gt;&lt;/a&gt; is another rxjs operator that combines multiple streams and executes when any one of the Observables emit a value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98p5f49raamr7v1f9idg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98p5f49raamr7v1f9idg.png" alt="Image that shows the following code block  raw `&amp;lt;br&amp;gt;
this.form.controls['field2'].valueChanges&amp;lt;br&amp;gt;
            .pipe(combineLatestWith(this.form.controls['field1'].valueChanges))&amp;lt;br&amp;gt;
            .subscribe({&amp;lt;br&amp;gt;
                next: ([field1, field2]) =&amp;gt; {&amp;lt;br&amp;gt;
                    // Do something with field1 and field2&amp;lt;br&amp;gt;
                },&amp;lt;br&amp;gt;
            });&amp;lt;br&amp;gt;
` endraw "&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Both are useful in different scenarios
&lt;/h2&gt;

&lt;p&gt;Here is what I mean by that - &lt;/p&gt;

&lt;p&gt;With &lt;code&gt;withLatestFrom&lt;/code&gt;, the subscription is executed only when &lt;code&gt;field2&lt;/code&gt; undergoes a value change and &lt;code&gt;field1&lt;/code&gt; has a value. So if you just change the value of &lt;code&gt;field1&lt;/code&gt;, nothing happens.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;combineLatestWith&lt;/code&gt;, the subscription is executed when &lt;strong&gt;any&lt;/strong&gt; of the controls undergo value changes. This can be problematic if you're running a lot of code in the subscription execution. So, use it with caution!&lt;/p&gt;

&lt;p&gt;The videos below show what I mean.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/6HZCGEXtyoU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VnCs2o7SsiE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you want to try the code for yourself, it's up &lt;a href="https://github.com/rajsaha/listeningToMultipleFormControls" rel="noopener noreferrer"&gt;here&lt;/a&gt; on GitHub.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
