<?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: Jacques Begin</title>
    <description>The latest articles on DEV Community by Jacques Begin (@jacquesbegin).</description>
    <link>https://dev.to/jacquesbegin</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%2F296044%2Fc519bee5-c7e8-40f6-a208-8ecb943adaf9.jpeg</url>
      <title>DEV Community: Jacques Begin</title>
      <link>https://dev.to/jacquesbegin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jacquesbegin"/>
    <language>en</language>
    <item>
      <title>Angular Binding Refactor, get rid of the extra code</title>
      <dc:creator>Jacques Begin</dc:creator>
      <pubDate>Wed, 18 Dec 2019 20:42:44 +0000</pubDate>
      <link>https://dev.to/jacquesbegin/angular-binding-refactor-get-rid-of-the-extra-code-218j</link>
      <guid>https://dev.to/jacquesbegin/angular-binding-refactor-get-rid-of-the-extra-code-218j</guid>
      <description>&lt;p&gt;I've been getting started in the Angular world and have really been impressed how it can reduce the amount of code for simple tasks. &lt;/p&gt;

&lt;p&gt;While working through the binding section of &lt;a href="https://www.udemy.com/course/the-complete-guide-to-angular-2/"&gt;"Angular 8 - The Complete Guide"&lt;/a&gt; (course on Udemy by Maximilian Schwarzmüller), I learned that by adding code to the bindings themselves, a fair bit of code could be eliminated in the component's typescript file.&lt;/p&gt;

&lt;p&gt;To practice using bindings I created:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input field to take a username (with ngModel to update the username value)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element to output the name (using string interpolation)&lt;/li&gt;
&lt;li&gt;Button to clear the input field (keep disabled if username is empty)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The component's html file looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt;
&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
&lt;span class="na"&gt;(input)=&lt;/span&gt;&lt;span class="s"&gt;"onInputUsername($event)"&lt;/span&gt;
&lt;span class="na"&gt;[(ngModel)]=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{ username }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
&lt;span class="na"&gt;[disabled]=&lt;/span&gt;&lt;span class="s"&gt;"usernameEmpty"&lt;/span&gt;
&lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"onClickClearUsername()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The input event called &lt;code&gt;onInputUsername($event)&lt;/code&gt; and was used to check if the input had a value and set a boolean accordingly.&lt;/p&gt;

&lt;p&gt;The button event called &lt;code&gt;onClickUsername()&lt;/code&gt; and was used to clear the input field when clicked.&lt;/p&gt;

&lt;p&gt;You can see these in action in the component's typescript file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BindingComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;usernameEmpty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;onInputUsername&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usernameEmpty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usernameEmpty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;onClickClearUsername&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usernameEmpty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As written, the code accomplished what I had intended; take in a username, display the username, and remove the username. But upon refactoring the code, it became apparent that Angular is capable of doing much of this work right in the bindings themselves.&lt;/p&gt;

&lt;p&gt;Instead of creating events to call component functions all that was necessary was to set property values as part of those bindings.&lt;/p&gt;

&lt;p&gt;The input's event could be removed completely and instead just be responsible for receiving input. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input &lt;br&gt;
type="text"&lt;br&gt;
[(ngModel)]="username"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The button's disabled property could be bound directly to the value of &lt;code&gt;username&lt;/code&gt; thus triggering it's active state when username had a value. Also, the click event would no longer need to call &lt;code&gt;onClickClearUsername()&lt;/code&gt; and instead set the value of &lt;code&gt;username&lt;/code&gt; to &lt;code&gt;""&lt;/code&gt; (empty string) when fired.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;button&lt;br&gt;
[disabled]="username === ''"&lt;br&gt;
(click)="username = ''"&amp;gt;Submit&amp;lt;/button&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check out the refactored work!&lt;/p&gt;

&lt;p&gt;HTML file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt;
&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
&lt;span class="na"&gt;[(ngModel)]=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{ username }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
&lt;span class="na"&gt;[disabled]=&lt;/span&gt;&lt;span class="s"&gt;"username === ''"&lt;/span&gt;
&lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"username = ''"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Typescript file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BindingComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By moving most of the work into the bindings the typescript file was reduced down to 1 property and an unnecessary event was removed from the HTML.&lt;/p&gt;

&lt;p&gt;The "magic" of Angular is a beautiful thing  🧙‍♂️🔮&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>refactor</category>
      <category>angular</category>
    </item>
  </channel>
</rss>
