<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hazar Askari on Medium]]></title>
        <description><![CDATA[Stories by Hazar Askari on Medium]]></description>
        <link>https://medium.com/@hazaraskari?source=rss-e34a3b6986ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EuO96hy4uM4rO2TI</url>
            <title>Stories by Hazar Askari on Medium</title>
            <link>https://medium.com/@hazaraskari?source=rss-e34a3b6986ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:44:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hazaraskari/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to write reusable components in Angular]]></title>
            <link>https://medium.com/@hazaraskari/how-to-write-reusable-components-in-angular-eba7b8c9a010?source=rss-e34a3b6986ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/eba7b8c9a010</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[reuseable]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[angular-7]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Hazar Askari]]></dc:creator>
            <pubDate>Sun, 17 Feb 2019 16:34:14 GMT</pubDate>
            <atom:updated>2019-02-17T19:08:07.300Z</atom:updated>
            <content:encoded><![CDATA[<p>This is going to be a short and sweet article on how we write reusable and highly scaleable components in Angular. Before we dig into code, lets create a hypothetical reusable component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KAoCVEwTbpP1Ei9zWQSwtg.png" /></figure><p><strong>The component we are going to build:<br></strong>We are going to create a reusable button component with a button text and icon. The button can actually have two icons, one on each side. Here are the functionality requirements list format.</p><ol><li>Button <em>can</em> have an icon of choice on the left side (must be able to show/hide). The parent decides the icon.</li><li>Button <em>can</em> have an icon of choice on the right side (must be able to show/hide). The parent decides the icon.</li><li>Button <em>can</em> change button-text</li><li>Button will emit an event to parent on click</li></ol><p>Now that we know what our component is going to be, we can start coding. Before we do that, i want you to stop reading and think about how you would go about creating this component. What would you do? How would you code this to be highly reusable, maintainable and scaleable? How would you make sure the component is easy to extend in the future? Give yourself a few minutes and think about it…</p><p>Done? OK great. I’m going to make a prediction about how you would structure you code. I’ve been a javascript developer for some years, and I’ve seen a lot of ways people approach this problem. I bet you ended up with something like the code below?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e064fb0c113c0f8466736915cc861b82/href">https://medium.com/media/e064fb0c113c0f8466736915cc861b82/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa3e319ed39e5647545367ddd5c29fb2/href">https://medium.com/media/fa3e319ed39e5647545367ddd5c29fb2/href</a></iframe><p>The code above shows how we usually end up writing our reusable components. The example shows two code snippets, one is the component and the other is how we call the component from the AppComponent. This is called “the configuration” approach, meaning we create variables for each configuration we need such as iconRight or showIconRight to accommodate the components needs. This approach is fine, and will work, but its not very extendable or maintainable is it? What if we need more configurations in the future? Do we keep adding more input properties? It can get messy, and hard to extend and maintain.</p><h3>Writing reusable components using &lt;ng-content&gt;</h3><p>Ladies and gentleman, i would like to introduce the correct way of writing reusable and highly maintainable/extendable components using ng-content. Think of ng-content as a slot or a placeholder for content being injected from the parent. With this approach we can pass html from the parent component to the child component. Lets see how this works with code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9202017c8e777f2017823fd2c1bffc23/href">https://medium.com/media/9202017c8e777f2017823fd2c1bffc23/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ec41f11a1bee3375b5b6f1780a86f29/href">https://medium.com/media/7ec41f11a1bee3375b5b6f1780a86f29/href</a></iframe><p>If you compare the examples above, you will notice that we do not pass any inputs to our highly reusable component. Instead of passing inputs, we added a ng-content tag and rather send in the html we want to render from our parent component. This means we achieve our goals in a much simpler and more maintainable/extendable way. We could easily extend this component with additional functionality without adding a lot more complexity. We also get much less code, which is always nice.</p><p>In conclusion, we want to create our reusable components using ng-content rather than sending a whole bunch of inputs to configure the component. This will make our components easily extendable and maintainable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eba7b8c9a010" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>