<?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: Nation Chirara</title>
    <description>The latest articles on DEV Community by Nation Chirara (@chiraranw).</description>
    <link>https://dev.to/chiraranw</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%2F125154%2Ff07a3321-eaf3-4055-b883-ee9c9c573107.jpeg</url>
      <title>DEV Community: Nation Chirara</title>
      <link>https://dev.to/chiraranw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chiraranw"/>
    <language>en</language>
    <item>
      <title>Angular Re-usable Components - (@Input) Decorator</title>
      <dc:creator>Nation Chirara</dc:creator>
      <pubDate>Thu, 12 Dec 2019 08:44:07 +0000</pubDate>
      <link>https://dev.to/chiraranw/angular-re-usable-components-input-decorator-5egk</link>
      <guid>https://dev.to/chiraranw/angular-re-usable-components-input-decorator-5egk</guid>
      <description>&lt;h2&gt;
  
  
  Reusable components with Angular
&lt;/h2&gt;

&lt;p&gt;One of the fundamental concepts that I learned about system design is to have/build reusable components. This approach saves time (building and testing the components) and writing boilerplate code. &lt;br&gt;
I am learning Angular and I have found how to build re-usable components in Angular. In Angular, re-usable components are built using two approaches depending on the requirement, a good number of times these are combined.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; @Input() – decorator (Part of Component API)&lt;/li&gt;
&lt;li&gt; Ng-content directive (Content Projection)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this article, I am going to show how to use the first approach to achieve component re-usability with &lt;strong&gt;@Input() – decorator&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@Input() – decorator&lt;/strong&gt; - allows you to pass data from parent component to child component. You will not be able to pass HTML. I have taken Bootstrap Card component for this example, and I intend to use this component in multiple places on my site but with different/dynamic headings. Instead of having multiple Cards with different headings, I am going to have one Card. This card is designed in such a way that the consumer/user of the component/card can pass in the heading at the time of calling the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnInit, Input } from '@angular/core';
@Component({
  selector: 'app-re-usable-card',
  templateUrl: './re-usable-card.component.html',
  styleUrls: ['./re-usable-card.component.css']
})
export class ReUsableCardComponent implements OnInit {
  @Input() heading: string;
  constructor() { }
  ngOnInit() {
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;@Input() heading: string;&lt;/code&gt; This line uses &lt;strong&gt;@Input()&lt;/strong&gt; decorator to mark the property &lt;em&gt;heading&lt;/em&gt; as one that can be passed on dynamically, making &lt;em&gt;ReUsableCardComponent&lt;/em&gt; re-usable. Do not forget to import &lt;strong&gt;Input&lt;/strong&gt;. Now the template for our re-usable component looks like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="card"&amp;gt;
    &amp;lt;div class="card-header"&amp;gt;
        {{heading}}
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="card-body"&amp;gt;
        &amp;lt;h5 class="card-title"&amp;gt;Special title treatment&amp;lt;/h5&amp;gt;
        &amp;lt;p class="card-text"&amp;gt;With supporting text below as a natural lead-in to additional content.&amp;lt;/p&amp;gt;
        &amp;lt;a href="#" class="btn btn-primary"&amp;gt;Go somewhere&amp;lt;/a&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;{{heading}}&lt;/code&gt; - binds the property heading of our component through string interpolation. Whatever that is passed from parent component targeting heading will be rendered inside this div&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="card-header"&amp;gt;
        //will be put here!
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The below code shows how we pass data from the parent component to our child component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="row"&amp;gt;
        &amp;lt;app-re-usable-card [heading]="'Heading 1'"&amp;gt;&amp;lt;/app-re-usable-card&amp;gt;
        &amp;lt;app-re-usable-card heading='Heading 2'&amp;gt;&amp;lt;/app-re-usable-card&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you notice that in the &lt;code&gt;card-body div&lt;/code&gt; of our component there is HTML content? Is there a way of dynamically injecting/putting that content for re-usability? Yes, that is the job of &lt;strong&gt;ng-content&lt;/strong&gt;, I will look into that in the next article. Thank you for reading, I appreciate comments!&lt;/p&gt;

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