<?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>Forem: Anubhab Mukherjee</title>
    <description>The latest articles on Forem by Anubhab Mukherjee (@anubhab5).</description>
    <link>https://forem.com/anubhab5</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%2F316143%2F66582893-21e2-4aa7-b8c2-41968051aaec.jpeg</url>
      <title>Forem: Anubhab Mukherjee</title>
      <link>https://forem.com/anubhab5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anubhab5"/>
    <language>en</language>
    <item>
      <title>ContentChild and ContentChildren in Angular</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Tue, 25 Jan 2022 18:21:24 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/contentchild-and-contentchildren-in-angular-3bne</link>
      <guid>https://forem.com/playfulprogramming-angular/contentchild-and-contentchildren-in-angular-3bne</guid>
      <description>&lt;p&gt;Today we will learn about &lt;code&gt;ContentChild&lt;/code&gt; and &lt;code&gt;ContentChildren&lt;/code&gt; in Angular.&lt;/p&gt;

&lt;p&gt;On a high level &lt;code&gt;ContentChild&lt;/code&gt; and &lt;code&gt;ContentChildren&lt;/code&gt; are property decorators. They are used to query or helps to get a reference to the projected content. If you are not aware of Content Projection I would highly recommend you to go through &lt;a href="https://dev.to/this-is-angular/content-projection-in-angular-1n9b"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lets see our playground first. &lt;br&gt;
We have a &lt;code&gt;MyCardsComponent&lt;/code&gt; component where few items are projected from the parent component (AppComponent).&lt;br&gt;
&lt;code&gt;The parent Component Template Code&lt;/code&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
    &amp;lt;div #header ngProjectAs='header'&amp;gt;Card Header&amp;lt;/div&amp;gt;
    &amp;lt;span id='card-sub-header'&amp;gt;Card Sub Header&amp;lt;/span&amp;gt;
    &amp;lt;div class="card-body"&amp;gt;
        This is a card Body!!!
    &amp;lt;/div&amp;gt;
    &amp;lt;footer title="card-footer"&amp;gt;
        Card Footer.
    &amp;lt;/footer&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;The Child Component Code&lt;/code&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ng-content select='header'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='#card-sub-header'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='.card-body'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='[title]'&amp;gt;&amp;lt;/ng-content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output till now -&lt;br&gt;
&lt;a href="https://media2.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%2F5soqkaea6ui9fhfwiy84.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5soqkaea6ui9fhfwiy84.PNG" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we will try to get the reference of the projected content in the Child Component to do some manipulation (say adding some style). In this scenario &lt;a href="https://dev.to/this-is-angular/understanding-viewchild-and-viewchildren-in-angular-1aep"&gt;ViewChild&lt;/a&gt; won't be helpful as it will not work. For this we need a new decorator called &lt;code&gt;ContentChild&lt;/code&gt;/ &lt;code&gt;ContentChildren&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;Lets paste in the below code in the &lt;code&gt;MyCardsComponent&lt;/code&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ContentChild('header')
  cardHeaderData: ElementRef = {
    nativeElement: undefined
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here in the above code we are defining a property &lt;code&gt;cardHeaderData&lt;/code&gt; and decorating with &lt;code&gt;ContentChild&lt;/code&gt;&lt;br&gt;
cardHeaderData is of type &lt;code&gt;ElementRef&lt;/code&gt; (A wrapper around the native element inside of a View)&lt;br&gt;
Now the next question can come - okay we can access the element but where we can get hold of the element for the first time and how to prove that we got hold of the element? &lt;br&gt;
For this there is another lifecycle hook provided by Angular - the &lt;code&gt;ngContentInit()&lt;/code&gt;.&lt;br&gt;
This method is called once the projected content is initialized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
Projected content will be accessible for the first time in the &lt;code&gt;ngAfterContentInit&lt;/code&gt; lifecycle hook method.&lt;/p&gt;

&lt;p&gt;So lets implement the function and see how it looks like. Paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ngAfterContentInit() {
    this.cardHeaderData
    debugger;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the devtool when we inspect we can see the below -&lt;br&gt;
&lt;a href="https://media2.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%2Fkj0g7u45sls218opsaoq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkj0g7u45sls218opsaoq.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
Here 👆🏻 we can see that the reference of the element (which was projected) we can get using the &lt;code&gt;ContentChild&lt;/code&gt; decorator and its a native element.&lt;br&gt;
Once we get hold of the element we can do manipulation like adding a style programmatically and many more cool things. To change the style lets add the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ngAfterContentInit() {
    this.cardHeaderData.nativeElement.style.color = 'blue';
    this.cardHeaderData.nativeElement.style.backgroundColor = 
'yellow';
    this.cardHeaderData.nativeElement.style.fontSize = '24px';
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you will see the below output -&lt;br&gt;
&lt;a href="https://media2.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%2F63r58ti75h5grie7fnkm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F63r58ti75h5grie7fnkm.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
So using the property we can target the nativeElement and set the color and do all the DOM tricks.&lt;/p&gt;

&lt;p&gt;Here we are targeting a HTML element (like div), but lets see what if we project a &lt;code&gt;Component&lt;/code&gt; how to access that.&lt;/p&gt;

&lt;p&gt;So lets create a component &lt;code&gt;ContentChildDemo&lt;/code&gt;. You should be a ninja by now to create a component using CLI &amp;amp; even if you are 1 step away from becoming a ninja you can follow &lt;a href="https://dev.to/anubhab5/creating-angular-component-129e"&gt;this post&lt;/a&gt;.&lt;br&gt;
And use the selector in &lt;code&gt;app.component.html&lt;/code&gt; file like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
 &amp;lt;app-content-child-demo&amp;gt;&amp;lt;/app-content-child-demo&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; in the &lt;code&gt;my-cards.component.ts&lt;/code&gt; file lets add the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see the below output. &lt;br&gt;
&lt;a href="https://media2.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%2Fspb71g7c6hidbmsppa7f.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fspb71g7c6hidbmsppa7f.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
So the content-projection is working 😊&lt;br&gt;
Now lets create a property and decorate with ContentChild.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ContentChild(ContentChildDemoComponent)
  contentChildDemoProperty: ContentChildDemoComponent | undefined;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here above you can see the ContentChildDecorator is accepting the name of the component you are trying to reference (In this case ContentChildDemoComponent), but in the first demo we were passing the reference (header) &lt;br&gt;
&lt;a href="https://media2.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%2F29ciyh26gch8etbc6o96.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F29ciyh26gch8etbc6o96.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;:&lt;br&gt;
1️⃣ When accessing Component we just pass the name of the component.&lt;br&gt;
2️⃣ When accessing a projected component, using the property you can even call a method present inside that projected content component.&lt;br&gt;
3️⃣ When accessing a native element we need to add a &lt;strong&gt;reference&lt;/strong&gt; and pass the same reference to the &lt;code&gt;ContentChild&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now lets understand when the &lt;code&gt;ContentChildren&lt;/code&gt; comes into play. &lt;br&gt;
Say in the above example we are projecting (passing from the parent) only 1 &lt;code&gt;ContentChildDemoComponent&lt;/code&gt;. But what if a scenario arises where you are passing multiple components and you need to access them?&lt;/p&gt;

&lt;p&gt;Something like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
 &amp;lt;app-content-child-demo&amp;gt;&amp;lt;/app-content-child-demo&amp;gt;
 &amp;lt;app-content-child-demo&amp;gt;&amp;lt;/app-content-child-demo&amp;gt;
 &amp;lt;app-content-child-demo&amp;gt;&amp;lt;/app-content-child-demo&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above case &lt;code&gt;ContentChild&lt;/code&gt; will return only the first match (very important remember this point).&lt;br&gt;
If you want to get hold of all the components projected you need to use the &lt;code&gt;ContentChildren&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;Lets add a new property like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ContentChildren(ContentChildDemoComponent)
  contentChildrenDemoProperty: 
   QueryList&amp;lt;ContentChildDemoComponent&amp;gt; | undefined;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the &lt;code&gt;ngAfterContentInit&lt;/code&gt; method -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ngAfterContentInit() {
    this.contentChildrenDemoProperty
    debugger;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if we run the application and debug we will see the below -&lt;br&gt;
&lt;a href="https://media2.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%2F1wqxysad3wkxy2ji8e5f.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1wqxysad3wkxy2ji8e5f.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
Here above we can see a &lt;code&gt;QueryList&lt;/code&gt; (An unmodifiable list) is being returned. You can loop through and access every item. Same powerful heavy lifting you can do on all the matching items in the QueryList.&lt;/p&gt;

&lt;p&gt;That's all for now.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post&lt;/p&gt;

&lt;p&gt;If you liked it please &lt;em&gt;like&lt;/em&gt; ❤️ &lt;em&gt;share&lt;/em&gt; 💞 &lt;em&gt;comment&lt;/em&gt; 🧡.&lt;/p&gt;

&lt;p&gt;Coming up &lt;code&gt;ChangeDetection&lt;/code&gt;&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt; tips and tricks.&lt;/p&gt;

&lt;p&gt;So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Cheers&lt;/em&gt;&lt;/strong&gt; 🍻 &lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Content Projection in Angular</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sat, 22 Jan 2022 18:29:37 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/content-projection-in-angular-1n9b</link>
      <guid>https://forem.com/playfulprogramming-angular/content-projection-in-angular-1n9b</guid>
      <description>&lt;p&gt;Today we will learn about a very important concept called &lt;code&gt;Content Projection&lt;/code&gt;. It's very useful concept and helps to make an application dynamic.&lt;/p&gt;

&lt;p&gt;Let's dive in by creating our playground first -&lt;br&gt;
Lets create a component called &lt;code&gt;my-cards&lt;/code&gt; and use it in the &lt;code&gt;app.component.html&lt;/code&gt; file (Hint 😉 using the selector)&lt;br&gt;
&lt;a href="https://media2.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%2Fp3svdhrx92pevx5y7051.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fp3svdhrx92pevx5y7051.PNG" alt=" " width="616" height="144"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Now lets try out a simple exercise. Add the below code in the &lt;code&gt;app.component.html&lt;/code&gt; file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
    &amp;lt;span&amp;gt;
        This is some content in between the card selector!
    &amp;lt;/span&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you open the browser to check the output you will see the text you typed in between the selector i.e. &lt;code&gt;This is some content in between the card selector!&lt;/code&gt; is not visible/ not getting displayed.&lt;/p&gt;

&lt;p&gt;The output -&lt;br&gt;
&lt;a href="https://media2.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%2Fn881ut4xqdv6lzoh70xf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn881ut4xqdv6lzoh70xf.PNG" alt=" " width="331" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the span element which is the child of the selector is the &lt;code&gt;content&lt;/code&gt; and it's also the child element. So if we combine both the term we get &lt;code&gt;content child&lt;/code&gt;. The span element is the content child.&lt;/p&gt;

&lt;p&gt;Now as we saw earlier in the demo the content child is not getting displayed. So in order to display the content child we need to project it. Or in simple term we need to have a special placeholder (in the child component - &lt;code&gt;MyCardsComponent&lt;/code&gt;) which will catch/ receive the value and display it.&lt;/p&gt;

&lt;p&gt;This special placeholder is &lt;code&gt;ng-content&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So now let's update the &lt;code&gt;MyCardsComponent&lt;/code&gt;'s template to have-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now you will see the below output -&lt;br&gt;
&lt;a href="https://media2.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%2F9vxij4i958mq7tryee6x.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9vxij4i958mq7tryee6x.PNG" alt=" " width="399" height="175"&gt;&lt;/a&gt;&lt;br&gt;
So what exactly happens ?&lt;br&gt;
The child content here in this case the &lt;code&gt;span&lt;/code&gt; gets projected in the &lt;code&gt;ng-content&lt;/code&gt;. So &lt;code&gt;ng-content&lt;/code&gt; acts as a placeholder.&lt;/p&gt;

&lt;p&gt;Now what if you need multiple placeholders? For example you will pass a content that would sit on the card-header another content as the card-body and another in the card-footer???&lt;br&gt;
For that we need to use something called &lt;code&gt;select&lt;/code&gt; - It is very powerful.&lt;br&gt;
The select can accept a &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;attribute&lt;/code&gt; or an &lt;code&gt;element&lt;/code&gt;. Confused?&lt;/p&gt;

&lt;p&gt;Lets see a quick example. Paste in the below code in the &lt;code&gt;app.component.html&lt;/code&gt; file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
    &amp;lt;header&amp;gt;Card Header&amp;lt;/header&amp;gt;
    &amp;lt;span id='card-sub-header'&amp;gt;Card Sub Header&amp;lt;/span&amp;gt;
    &amp;lt;div class="card-body"&amp;gt;
        This is a card Body!!!
    &amp;lt;/div&amp;gt;
    &amp;lt;footer title="card-footer"&amp;gt;
        Card Footer.
    &amp;lt;/footer&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the &lt;code&gt;my-cards.component.html&lt;/code&gt; file i.e. &lt;code&gt;MyCardsComponent&lt;/code&gt;'s template file paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ng-content select='header'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='#card-sub-header'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='.card-body'&amp;gt;&amp;lt;/ng-content&amp;gt;
&amp;lt;ng-content select='[title]'&amp;gt;&amp;lt;/ng-content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output - &lt;br&gt;
&lt;a href="https://media2.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%2Fg9rzv54uxcja2qzrfae5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg9rzv54uxcja2qzrfae5.PNG" alt=" " width="328" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the 1️⃣ first &lt;code&gt;ng-content&lt;/code&gt; has a selector which matches an element/ tag - the header tag&lt;br&gt;
2️⃣ Second &lt;code&gt;ng-content&lt;/code&gt; selector matches an id &lt;code&gt;card-sub-header&lt;/code&gt;&lt;br&gt;
3️⃣ Third &lt;code&gt;ng-content&lt;/code&gt; selector matches a class &lt;code&gt;card-body&lt;/code&gt;&lt;br&gt;
4️⃣ Fourth &lt;code&gt;ng-content&lt;/code&gt; selector matches an attribute &lt;code&gt;title&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There can also be a scenario where instead of using header tag (in the above example) you need to use a div tag like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
    &amp;lt;div &amp;gt;Card Header&amp;lt;/div&amp;gt;
    &amp;lt;span id='card-sub-header'&amp;gt;Card Sub Header&amp;lt;/span&amp;gt;
    &amp;lt;div class="card-body"&amp;gt;
        This is a card Body!!!
    &amp;lt;/div&amp;gt;
    &amp;lt;footer title="card-footer"&amp;gt;
        Card Footer.
    &amp;lt;/footer&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the header will not work right? And suppose you don't have the option to change the card component also. Then what's the solution ???&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ngProjectAs&lt;/code&gt; comes to the rescue!!!&lt;/p&gt;

&lt;p&gt;Paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-cards&amp;gt;
    &amp;lt;div ngProjectAs='header'&amp;gt;Card Header&amp;lt;/div&amp;gt;
    &amp;lt;span id='card-sub-header'&amp;gt;Card Sub Header&amp;lt;/span&amp;gt;
    &amp;lt;div class="card-body"&amp;gt;
        This is a card Body!!!
    &amp;lt;/div&amp;gt;
    &amp;lt;footer title="card-footer"&amp;gt;
        Card Footer.
    &amp;lt;/footer&amp;gt;
&amp;lt;/app-my-cards&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see the first line is the &lt;code&gt;div&lt;/code&gt; tag. And we have used &lt;code&gt;ngProjectAs='header'&lt;/code&gt;. In this case Angular will consider the selector as &lt;code&gt;header&lt;/code&gt; and the output will work as it is.&lt;/p&gt;

&lt;p&gt;That's all for now.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post&lt;/p&gt;

&lt;p&gt;If you liked it please &lt;em&gt;like&lt;/em&gt; ❤️ &lt;em&gt;share&lt;/em&gt; 💞 &lt;em&gt;comment&lt;/em&gt; 🧡.&lt;/p&gt;

&lt;p&gt;Coming up &lt;code&gt;ContentChild&lt;/code&gt; and &lt;code&gt;ContentChildren&lt;/code&gt;.&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt; tips and tricks.&lt;/p&gt;

&lt;p&gt;So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Cheers&lt;/em&gt;&lt;/strong&gt; 🍻 &lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding ViewChild and ViewChildren in Angular</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Fri, 21 Jan 2022 12:12:08 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/understanding-viewchild-and-viewchildren-in-angular-1aep</link>
      <guid>https://forem.com/playfulprogramming-angular/understanding-viewchild-and-viewchildren-in-angular-1aep</guid>
      <description>&lt;p&gt;Today we will learn a very important and powerful concept in Angular - the &lt;code&gt;ViewChild&lt;/code&gt; and &lt;code&gt;ViewChildren&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On a high level &lt;code&gt;ViewChild&lt;/code&gt; and &lt;code&gt;ViewChildren&lt;/code&gt; both are property decorators. &lt;br&gt;
With this decorators we can -&lt;br&gt;
 1️⃣ Access an element present in the same template (html) of the component&lt;br&gt;
 2️⃣ Access the child component&lt;/p&gt;

&lt;p&gt;So we can say it's used to access elements present in the template.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;@ViewChild(selector, {read: readValue, static: staticValue})&lt;br&gt;
propertyName&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I will touch upon the syntax as and when we need them.&lt;/p&gt;

&lt;p&gt;So what we will achieve at the end of this demo?&lt;br&gt;
We will create a counter component. It will have a counter initialized to 0. There will be two methods one to increase the counter by &lt;code&gt;x&lt;/code&gt; amount and the other to reduce by &lt;code&gt;x&lt;/code&gt; amount.&lt;br&gt;
The component methods will be accessed or you can say called from the parent only.&lt;/p&gt;

&lt;p&gt;Lets setup our playground first.&lt;br&gt;
Lets create a component called &lt;code&gt;my-counter&lt;/code&gt; and make it a child component of &lt;code&gt;app-component&lt;/code&gt;. If you are wondering how to make a child component? Please have a look at this &lt;a href="https://dev.to/this-is-angular/component-communication-parent-to-child-child-to-parent-5800"&gt;post&lt;/a&gt; before moving ahead.&lt;/p&gt;

&lt;p&gt;Once we create the component, lets open the my-counter component ts file and add the below code-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  counter = 0;
  constructor() { }
  ngOnInit(): void { }

  increaseCounter(x: number) {
    this.counter += x;
  }

  decreaseCounter(x: number) {
    this.counter -= x;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F8ozorey0kxpbhuvzjqjn.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8ozorey0kxpbhuvzjqjn.PNG" alt=" " width="707" height="646"&gt;&lt;/a&gt;&lt;br&gt;
And in the corresponding template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;Counter Value: {{ counter }}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets move to the app.component.ts file and the following code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  increaseCounter(x: number) { }

  decreaseCounter(x: number) { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will add the method body soon.&lt;br&gt;
And in the corresponding template the following code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input (click)="increaseCounter(1)" type="button" 
value="Add by 1"&amp;gt;

&amp;lt;input (click)="decreaseCounter(1)" type="button" 
value="Subtract by 1"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Output till now&lt;/em&gt; - &lt;br&gt;
&lt;a href="https://media2.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%2Ftha8z81pr0u7mm7lyk8q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftha8z81pr0u7mm7lyk8q.PNG" alt=" " width="295" height="199"&gt;&lt;/a&gt;&lt;br&gt;
And the button does nothing. But it will do 🤞&lt;/p&gt;

&lt;p&gt;Now lets come to the real part -&lt;/p&gt;

&lt;p&gt;We will see ViewChild first.&lt;br&gt;
So lets add a property in &lt;code&gt;app.component&lt;/code&gt; 👉 &lt;code&gt;counterReference&lt;/code&gt;.&lt;br&gt;
The property counterReference will be holding a reference of the Counter component. So we will assign -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counterReference = {} as MyCounterComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and also decorate it with ViewChild. So the final code will become -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ViewChild(MyCounterComponent)
  counterReference = {} as MyCounterComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The viewChild accepts few parameters. &lt;br&gt;
The first parameter is the &lt;u&gt;Component&lt;/u&gt; which you want to select or get the reference (in this case). You can also query using a templateReference (which I will show soon).&lt;br&gt;
Now lets complete the 2 functions which we kept empty -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  increaseCounter(x: number) {
    this.counterReference.increaseCounter(1);
  }

  decreaseCounter(x: number) {
    this.counterReference.decreaseCounter(1);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the above code you can see with the property  &lt;code&gt;counterReference&lt;/code&gt; decorated with ViewChild we can access the child component &lt;code&gt;MyCounterComponent&lt;/code&gt; (methods).&lt;/p&gt;

&lt;p&gt;When you click any of the button you will see the Counter value is getting changed.&lt;br&gt;
&lt;a href="https://media2.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%2Fozn8nix4tvw8g5ddlwhb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fozn8nix4tvw8g5ddlwhb.PNG" alt=" " width="340" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So from the parent you can access the child methods.&lt;/p&gt;

&lt;p&gt;Cool right?&lt;br&gt;
Now the next variation (as I said earlier) using &lt;code&gt;template reference&lt;/code&gt;.&lt;br&gt;
Example of a template reference -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div #myTemplateRef&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;app-my-counter #componentTemplateRef&amp;gt;&amp;lt;/app-my-counter&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Observe the # (pound symbol).&lt;br&gt;
It is a variable to reference the DOM within a template. Here &lt;code&gt;myTemplateRef&lt;/code&gt; or &lt;code&gt;componentTemplateRef&lt;/code&gt; is the template reference.&lt;/p&gt;

&lt;p&gt;Lets add few more lines of code to see how it works. Add the below lines of code in component.ts file-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ViewChild('myTemplateRef')
  myTemplateRef = {};

  @ViewChild('componentTemplateRef')
  componentTemplateRef = {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the corresponding template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div #myTemplateRef&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;app-my-counter #componentTemplateRef&amp;gt;&amp;lt;/app-my-counter&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a very important piece of information -&lt;/p&gt;

&lt;p&gt;You might have seen a method that gets generated when you create a component using CLI - the &lt;code&gt;ngOnInit()&lt;/code&gt;&lt;br&gt;
This is a Angular lifecycle hook method. I will talk about the &lt;strong&gt;Lifecycle Hooks&lt;/strong&gt; in details in the upcoming post.&lt;br&gt;
Similar to this method there is another life cycle hook method called &lt;code&gt;ngAfterViewInit()&lt;/code&gt;.&lt;br&gt;
So, when ever the template/ view initialization is complete or I can say view is ready the &lt;code&gt;ngAfterViewInit()&lt;/code&gt; method is called and all the properties decorated with viewChild are ready to use. Before that they are uninitialized/ undefined.&lt;br&gt;
In the &lt;code&gt;ngOnInit&lt;/code&gt; the properties would look like below -&lt;br&gt;
&lt;a href="https://media2.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%2Fs9em6hppz1brglfcyxrk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs9em6hppz1brglfcyxrk.PNG" alt=" " width="304" height="325"&gt;&lt;/a&gt;&lt;br&gt;
And in the &lt;code&gt;ngAfterViewInit&lt;/code&gt; the properties would look like -&lt;br&gt;
&lt;a href="https://media2.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%2Fxf8nzmzltsh2k3rlqjr3.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxf8nzmzltsh2k3rlqjr3.PNG" alt=" " width="319" height="390"&gt;&lt;/a&gt;&lt;br&gt;
In summary a quick pictorial representation -&lt;br&gt;
&lt;a href="https://media2.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%2Fpij5fi0kifhmvzbqune3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpij5fi0kifhmvzbqune3.png" alt=" " width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;static:&lt;/strong&gt; &lt;br&gt;
By default the value of static is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The true value is used to support creating embedded view on the runtime. When I will write about creating dynamic component again I will talk about &lt;code&gt;static: true&lt;/code&gt;. &lt;/p&gt;



&lt;p&gt;Now coming to &lt;code&gt;ViewChildren&lt;/code&gt;. It is very similar to &lt;code&gt;ViewChild&lt;/code&gt; except it provides a collection of matching references as a QueryList of items.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QueryList&lt;/code&gt; - Its an unmodifiable list of items that Angular keeps track of and up to date when the state of the application changes.&lt;/p&gt;

&lt;p&gt;There are few properties and methods of QueryList&lt;br&gt;
&lt;code&gt;first&lt;/code&gt;: gets the first item in the list.&lt;br&gt;
&lt;code&gt;last&lt;/code&gt;: gets the last item in the list.&lt;br&gt;
&lt;code&gt;length&lt;/code&gt;: gets the length of the items.&lt;br&gt;
&lt;code&gt;changes&lt;/code&gt;: An observable. It emits a new value, whenever the Angular adds, removes or moves the child elements.&lt;/p&gt;

&lt;p&gt;JavaScript array methods like map(), filter() , find(), forEach(), etc. are also supported by the QueryList&lt;/p&gt;

&lt;p&gt;Now the example -&lt;br&gt;
You have three entries of the same component 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-my-counter&amp;gt;&amp;lt;/app-my-counter&amp;gt;
&amp;lt;app-my-counter&amp;gt;&amp;lt;/app-my-counter&amp;gt;
&amp;lt;app-my-counter&amp;gt;&amp;lt;/app-my-counter&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you want to get hold of all the items. &lt;br&gt;
&lt;code&gt;ViewChildren&lt;/code&gt; is the best choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @ViewChildren(MyCounterComponent)
  viewChildrenRef: QueryList&amp;lt;MyCounterComponent&amp;gt; | undefined;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here using ViewChildren you can get access to all the matching selector. You can loop through them and perform any operation you need to.&lt;/p&gt;

&lt;p&gt;That's all for now.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post&lt;/p&gt;

&lt;p&gt;If you liked it please &lt;em&gt;like&lt;/em&gt; ❤️ &lt;em&gt;share&lt;/em&gt; 💞 &lt;em&gt;comment&lt;/em&gt; 🧡.&lt;/p&gt;

&lt;p&gt;Coming up more topics on Angular.&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt; tips and tricks.&lt;/p&gt;

&lt;p&gt;So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Cheers&lt;/em&gt;&lt;/strong&gt; 🍻 &lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>development</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How Angular Application Starts - Behind The $cene story!</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sun, 16 Jan 2022 07:51:15 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/how-angular-application-works-behind-the-cene-story-1i21</link>
      <guid>https://forem.com/playfulprogramming-angular/how-angular-application-works-behind-the-cene-story-1i21</guid>
      <description>&lt;p&gt;Today we will learn the application flow in Angular. &lt;br&gt;
What happens when the application starts.&lt;/p&gt;
&lt;h2&gt;
  
  
  It would be a theory but again an important one if you want to master Angular.
&lt;/h2&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;angular.json&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;When you open up your angular project you might have noticed a file sitting at the project root - &lt;code&gt;angular.json&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fgkbl5l2977mlwritfe0h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgkbl5l2977mlwritfe0h.PNG" alt=" " width="325" height="323"&gt;&lt;/a&gt;&lt;br&gt;
It is a very important file and contains several important configuration information to run the angular application.&lt;br&gt;
A detailed overview I will provide on this very soon.&lt;/p&gt;

&lt;p&gt;When the application first starts Angular looks for this file.&lt;/p&gt;

&lt;p&gt;So now if you open the file you will find a node called &lt;code&gt;main&lt;/code&gt; under &lt;br&gt;
architect -&amp;gt; build -&amp;gt; options , you would see a main node&lt;br&gt;
&lt;a href="https://media2.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%2F4l3uo14c8zjg07tlsts9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4l3uo14c8zjg07tlsts9.PNG" alt=" " width="614" height="207"&gt;&lt;/a&gt;&lt;br&gt;
Once Angular found the file it starts looking for the main node.&lt;br&gt;
The value of the main node is a file path that Angular is looking for - the &lt;code&gt;main.ts&lt;/code&gt; under src folder.&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;main.ts&lt;/strong&gt;&lt;br&gt;
This is the main entry point. As an analogy if you have a prior knowledge of C/ C++/ Java you must have seen there also that main is the starting point.&lt;br&gt;
The &lt;code&gt;main.ts&lt;/code&gt; file is present under the &lt;code&gt;src&lt;/code&gt; folder.&lt;br&gt;
&lt;a href="https://media2.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%2F85pzrdnktsxx2ag6c87e.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F85pzrdnktsxx2ag6c87e.PNG" alt=" " width="312" height="523"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from 
'@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err =&amp;gt; console.error(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;platformBrowserDynamic&lt;/code&gt; creates the platform. This function is also used to bootstrap the Angular Application. Here we specify the first module that should be loaded when the application starts for the first time - the root module. In other words the bootstrapping module.&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;app.module.ts&lt;/strong&gt;&lt;br&gt;
Once angular finds the starting module the &lt;code&gt;app.module.ts&lt;/code&gt; (where all the components/ directives/ pipes associated to this module is present and dependency to other module is also present) it looks for the bootstrap option -&lt;br&gt;
&lt;a href="https://media2.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%2F4p3109dkii4368acbyp8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4p3109dkii4368acbyp8.PNG" alt=" " width="452" height="278"&gt;&lt;/a&gt;&lt;br&gt;
Where the starting component name has been specified - in this case the AppComponent.&lt;/p&gt;

&lt;p&gt;By now Angular compiler has all the required information it needs to work.&lt;/p&gt;

&lt;p&gt;4️⃣ &lt;strong&gt;index.html&lt;/strong&gt;&lt;br&gt;
Now the index.html is loaded and starts parsing. Once it finds the selector  it exactly knows which component to load and the AppComponent is displayed in the screen.&lt;/p&gt;

&lt;p&gt;And that's it. This is how the application starts.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post&lt;/p&gt;

&lt;p&gt;If you liked it please &lt;em&gt;like&lt;/em&gt; ❤️ &lt;em&gt;share&lt;/em&gt; 💞 &lt;em&gt;comment&lt;/em&gt; 🧡.&lt;/p&gt;

&lt;p&gt;Coming up more topics on Angular.&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt; tips and tricks.&lt;/p&gt;

&lt;p&gt;So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Cheers&lt;/em&gt;&lt;/strong&gt; 🍻 &lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>intermediate</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Component Communication in Angular (Parent to Child &amp; Child to Parent)</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sat, 15 Jan 2022 19:18:05 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/component-communication-parent-to-child-child-to-parent-5800</link>
      <guid>https://forem.com/playfulprogramming-angular/component-communication-parent-to-child-child-to-parent-5800</guid>
      <description>&lt;p&gt;Today we will learn one of the most important topic in Angular - how to communicate between two components when they have a parent child relationship. &lt;br&gt;
🔴 &lt;strong&gt;Prerequisite&lt;/strong&gt; 🔴&lt;br&gt;
You need to know (if not please follow the link associated)-&lt;br&gt;
🔵 What is a &lt;a href="https://dev.to/anubhab5/understanding-angular-component-2bpa"&gt;component&lt;/a&gt; in Angular&lt;br&gt;
🔵 How to create a &lt;a href="https://dev.to/anubhab5/creating-angular-component-129e"&gt;component&lt;/a&gt; in Angular&lt;/p&gt;

&lt;p&gt;Before we begin we need to understand what is the meaning of parent-child relation. Suppose you have a component called &lt;code&gt;P&lt;/code&gt;. In the template of the &lt;code&gt;P&lt;/code&gt; component you write selector of another component say &lt;code&gt;C&lt;/code&gt;. In this case the component &lt;code&gt;C&lt;/code&gt; is the child of the &lt;code&gt;P&lt;/code&gt; component or &lt;code&gt;P&lt;/code&gt; is the parent of &lt;code&gt;C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So now we know the theory so lets quickly setup our playground. &lt;/p&gt;

&lt;p&gt;We create two components, lets name the parent component as &lt;br&gt;
&lt;code&gt;movie-dashboard&lt;/code&gt; or easier way I would say &lt;code&gt;movie-dashboard&lt;/code&gt; component will act as the parent and &lt;code&gt;movie-table&lt;/code&gt; (our second component) will act as the child component. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The CLI commands&lt;/em&gt;:&lt;br&gt;
&lt;code&gt;ng g c movie-dashboard&lt;/code&gt;&lt;br&gt;
&lt;code&gt;ng g c movie-table&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2F31n71uf6n8bpif5lq3we.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F31n71uf6n8bpif5lq3we.PNG" alt=" " width="798" height="351"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Project Folder Structure&lt;/em&gt;:&lt;br&gt;
&lt;a href="https://media2.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%2Fgt1yd9zwpl1qs6m3ofny.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgt1yd9zwpl1qs6m3ofny.PNG" alt=" " width="422" height="439"&gt;&lt;/a&gt;&lt;br&gt;
Parent marked with yellow arrow 🟡&lt;br&gt;
Child marked with green arrow 🟢&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;app.component.html&lt;/code&gt; we paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-movie-dashboard&amp;gt;&amp;lt;/app-movie-dashboard&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;movie-dashboard.component.html&lt;/code&gt; file lets paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;movie-dashboard works!&amp;lt;/p&amp;gt;
&amp;lt;app-movie-table&amp;gt;&amp;lt;/app-movie-table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you start the application you should see the following output in the browser's localhost:4200&lt;br&gt;
&lt;a href="https://media2.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%2F5c73804uy6rj7yuxlm6i.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5c73804uy6rj7yuxlm6i.PNG" alt=" " width="307" height="176"&gt;&lt;/a&gt;&lt;br&gt;
So now our playground is ready. &lt;/p&gt;



&lt;p&gt;❔❔❔ Next big question what are we building ❔❔❔&lt;/p&gt;

&lt;p&gt;We will be building a simple flow where the parent component will be holding some movie names/ movie array (I will refer to as movie list from now on...). We will be &lt;strong&gt;passing&lt;/strong&gt; the movieList to the child component where we will be displaying the list in a tabular format. &lt;br&gt;
From the table the user can select a movie by clicking the button (&lt;em&gt;some user action&lt;/em&gt;)  which will be passed back to the parent (may be for some processing).&lt;/p&gt;

&lt;p&gt;So now the context is set. &lt;br&gt;
Lets see how we can pass data from parent to child. &lt;br&gt;
We pass data from parent to child using an &lt;strong&gt;Input decorator&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Decorator&lt;/strong&gt;&lt;/em&gt; we already have come across when we defined a component, directive, pipe, module remember??? (&lt;code&gt;@Component&lt;/code&gt;, &lt;code&gt;@Directive&lt;/code&gt;, &lt;code&gt;@Pipe&lt;/code&gt;, &lt;code&gt;@NgModule&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;All the above decorators are placed on the top of a class. They are also called &lt;code&gt;Class Decorator&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Input decorator is a property decorator. What does that mean???&lt;br&gt;
What ever variables you create directly inside a class (not inside a method) are all properties.&lt;br&gt;
And this Input decorator can be put only on the top of a property. So that is the reason it is called Property decorator (and a decorator always starts with @ so &lt;code&gt;@Input&lt;/code&gt;)&lt;br&gt;
Once you put a decorator it gets some special power like a super hero.&lt;br&gt;
So where should we put this decorator. Easy way I will tell you which will always stick to your mind -&lt;/p&gt;

&lt;p&gt;Which ever component will &lt;strong&gt;receive&lt;/strong&gt; the data should have the property decorated with &lt;code&gt;@Input&lt;/code&gt;. So in our case the &lt;code&gt;movie-table&lt;/code&gt; component will receive the data. So we need to create a property and mark it with the decorator by now you know which decorator we will use.&lt;br&gt;
So lets open the &lt;code&gt;movie-table.component.ts&lt;/code&gt; file and paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Input()
  movieList: Array&amp;lt;string&amp;gt; = [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fdw4jekd8o0wghfg24p2v.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdw4jekd8o0wghfg24p2v.PNG" alt=" " width="667" height="407"&gt;&lt;/a&gt;&lt;br&gt;
You can see line number 11 is the &lt;code&gt;movieList&lt;/code&gt; property. We have put the &lt;code&gt;@Input()&lt;/code&gt; on the line number 10. &lt;br&gt;
Although you can put in front of the property as well like below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Input() movieList: Array&amp;lt;string&amp;gt; = [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So once we use this decorator on a property the property is able to hold the data we pass from parent.&lt;/p&gt;

&lt;p&gt;By now we got some one who will hold the data in the child. But how to pass the data from parent. For that lets go to the &lt;code&gt;movie-dashboard.component.ts&lt;/code&gt; file and paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  myFavoriteMovies = [ 'Encanto', 
'Spider-Man: No Way Home', 
"Harry Potter and the Sorcerer's Stone" ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now come to the corresponding template file the &lt;code&gt;movie-dashboard.component.html&lt;/code&gt; and paste in the below code (by removing the old code present) -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;movie-dashboard works!&amp;lt;/p&amp;gt;
&amp;lt;app-movie-table [movieList]="myFavoriteMovies"&amp;gt;&amp;lt;/app-movie-table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have 2 points to note-&lt;br&gt;
1️⃣ The input decorator property we created in the child component is placed inside the square bracket []. Here &lt;code&gt;movieList&lt;/code&gt; is present inside the square bracket.&lt;br&gt;
2️⃣ The data which we want to pass to the child should be assigned to the above property. We use equal operator and the variable which contains the data. Here &lt;code&gt;myFavoriteMovies&lt;/code&gt; property contains the data which we have assigned.&lt;/p&gt;

&lt;p&gt;That's it now its ready &amp;amp; we learnt how to pass the data from parent to child. &lt;br&gt;
But wait I need to show it to you also right?&lt;br&gt;
So for that lets paste in the below code in the &lt;code&gt;movie-table.component.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;table&amp;gt;
    &amp;lt;tr&amp;gt;
      &amp;lt;th&amp;gt;Movie Name&amp;lt;/th&amp;gt;
      &amp;lt;th&amp;gt;&amp;lt;/th&amp;gt;
    &amp;lt;/tr&amp;gt;
    &amp;lt;tr *ngFor="let movie of movieList"&amp;gt;
      &amp;lt;td&amp;gt;{{movie}}&amp;lt;/td&amp;gt;
      &amp;lt;td&amp;gt;&amp;lt;input type="button" value="Select"&amp;gt;&amp;lt;/td&amp;gt;
    &amp;lt;/tr&amp;gt;
&amp;lt;/table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see the output as -&lt;br&gt;
&lt;a href="https://media2.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%2F4ljqbupklkfw0kmidmnu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4ljqbupklkfw0kmidmnu.PNG" alt=" " width="375" height="276"&gt;&lt;/a&gt;&lt;br&gt;
Wow! We are receiving the data in the child component.&lt;br&gt;
As I said earlier &lt;code&gt;movieList&lt;/code&gt; property will receive the data from the parent. Since we are passing an array we are just looping through it using &lt;a href="https://dev.to/anubhab5/understanding-built-in-angular-directives-part-5-gli"&gt;ngFor&lt;/a&gt; (If you are not aware of please have a look).&lt;/p&gt;

&lt;p&gt;Our First step is done - Passing data from parent to child.&lt;br&gt;
Now comes the second part. The user clicks the select button and the selected movie will be passed to the parent component.&lt;/p&gt;

&lt;p&gt;For that we will learn yet another new property decorator the&lt;br&gt;
Output decorator.&lt;/p&gt;

&lt;p&gt;So lets paste in the below code in the &lt;code&gt;movie-table.component.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Output()
  movieSelectedEventEmitter =  new EventEmitter();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fri5leqwthc8pmxqwxk4k.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fri5leqwthc8pmxqwxk4k.PNG" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;br&gt;
So here we are adding a property &lt;code&gt;movieSelectedEventEmitter&lt;/code&gt; and decorating with Output decorator. And now what is the remaining part?&lt;br&gt;
When ever the user clicks a button it actually raises an event which will emitted back to the parent. We are actually creating an object of it. So the property becomes super powerful (like our action hero) and can emit/ send the data to the parent component.&lt;/p&gt;

&lt;p&gt;But we also need to capture the click event when the user clicks the button. So lets create a method which will be called when the user clicks the button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  movieSelected(selectedMovie: string) { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing in the body yet. We will fill it soon!&lt;/p&gt;

&lt;p&gt;In the corresponding template file lets paste the below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;td&amp;gt;&amp;lt;input type="button" value="Select" 
(click)="movieSelected(movie)"&amp;gt;&amp;lt;/td&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fxo0kn4s15rb3rmv638cj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxo0kn4s15rb3rmv638cj.PNG" alt=" " width="800" height="266"&gt;&lt;/a&gt;&lt;br&gt;
All events we write inside a round bracket &lt;code&gt;()&lt;/code&gt;. &lt;br&gt;
Here &lt;code&gt;click&lt;/code&gt; is an inbuilt event.&lt;br&gt;
And once that event is fired/ triggered the function we pass just after the equal operator will be called.&lt;br&gt;
Now lets come back to the method which we just now created. And paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.movieSelectedEventEmitter.emit(selectedMovie);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F6xejffysooao85ls3d1l.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6xejffysooao85ls3d1l.PNG" alt=" " width="648" height="114"&gt;&lt;/a&gt;&lt;br&gt;
So what are we doing?&lt;br&gt;
On the Output property we created we are calling the emit (remember we assigned an EventEmitter object) and passing the data, in this case the selected movie name.&lt;/p&gt;

&lt;p&gt;But the job is still half done. In order to get the data in the parent we need to add few extra lines of code.&lt;br&gt;
So lets open the &lt;code&gt;movie-dashboard.component.html&lt;/code&gt; file and paste in the below code (and you can remove the old one)-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;movie-dashboard works!&amp;lt;/p&amp;gt;
&amp;lt;app-movie-table 
(movieSelectedEventEmitter)="selectedMovieToWatch($event)" 
[movieList]="myFavoriteMovies"&amp;gt;
&amp;lt;/app-movie-table&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F964ro26hyvk1t2md0age.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F964ro26hyvk1t2md0age.PNG" alt=" " width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are using the event emitter we created &lt;code&gt;movieSelectedEventEmitter&lt;/code&gt; (or the Output property inside a round bracket since its an event, same formula as the click we used few steps back) and assigning it to a method which will be called when the event is triggered (exactly same concept/ way as the click event, the only difference is click is an inbuilt one and this is a custom/ user-defined one)&lt;/p&gt;

&lt;p&gt;So lets define the &lt;code&gt;selectedMovieToWatch&lt;/code&gt; method.&lt;br&gt;
In the movie-dashboard.component file lets paste the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  selectedMovieToWatch(data: string) {
    debugger;
    alert(data);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here data will receive the value which we passed from the child.&lt;br&gt;
That's it. Now lets look at the output -&lt;br&gt;
I clicked the second item (trust me)&lt;br&gt;
&lt;a href="https://media2.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%2Fod2bhgxw3709d0lqyr6q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fod2bhgxw3709d0lqyr6q.PNG" alt=" " width="586" height="271"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fh1vmsdla6jr0hw81hyht.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fh1vmsdla6jr0hw81hyht.PNG" alt=" " width="800" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this brings to the end!!! I know it was a long post.&lt;/p&gt;
&lt;h2&gt;
  
  
  Highlights
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Input&lt;/code&gt;&lt;br&gt;
1️⃣ Need to decorate a property in the &lt;strong&gt;child component&lt;/strong&gt; as @Input()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Input()
  movieList: Array&amp;lt;string&amp;gt; = [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ In the Parent component where the child selector is used need to add the same input property in Square Bracket [] and assign the value to be passed&lt;br&gt;
&lt;code&gt;[movieList]="myFavoriteMovies"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Output&lt;/code&gt;&lt;br&gt;
1️⃣ Need to mark a property in the child component with the Output decorator and assign an EventEmitter Object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Output()
  movieSelectedEventEmitter = new EventEmitter();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ In the parent we need to add the event and assign to a method&lt;br&gt;
&lt;code&gt;(movieSelectedEventEmitter)="selectedMovieToWatch($event)"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post&lt;/p&gt;

&lt;p&gt;If you liked it please do &lt;em&gt;like&lt;/em&gt; ❤️ &lt;em&gt;share&lt;/em&gt; 💞 and &lt;em&gt;comment&lt;/em&gt; 🧡.&lt;/p&gt;

&lt;p&gt;Coming up more topics on Angular.&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt;&lt;br&gt;
So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>components</category>
      <category>programming</category>
    </item>
    <item>
      <title>Types of Angular Module</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Fri, 14 Jan 2022 17:44:35 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/types-of-angular-module-amd</link>
      <guid>https://forem.com/playfulprogramming-angular/types-of-angular-module-amd</guid>
      <description>&lt;p&gt;Today we will understand about the different types of modules you can have in an application. If you are not aware of Angular Modules then I would suggest you to go through this  &lt;a href="https://dev.to/this-is-angular/understanding-angular-modules-41pb"&gt;post&lt;/a&gt;. Also you need to know how to create your own module. For that you can have a look at this &lt;a href="https://dev.to/this-is-angular/creating-custom-module-in-angular-4p66"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;📣 &lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
This will be more of theory where we will understand the requirement of different types of modules. It can be different puzzle pieces for you as of now. But in coming posts I will cover all the module type with example. And then you will be able to see the puzzle completed. &lt;/p&gt;




&lt;p&gt;Before diving deep first lets understand why we need various types of modules. (By the way one of the module type we already saw in the earlier &lt;a href="https://dev.to/this-is-angular/creating-custom-module-in-angular-4p66"&gt;post&lt;/a&gt;). &lt;br&gt;
As your application grows in size you need to break/ group the application into various chunks for -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;maintainability&lt;/code&gt;, &lt;br&gt;
&lt;code&gt;enhance the performance&lt;/code&gt;, &lt;br&gt;
&lt;code&gt;scalability&lt;/code&gt;, &lt;br&gt;
&lt;code&gt;easy deployment&lt;/code&gt;, &lt;br&gt;
&lt;code&gt;enhance the development process&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To meet the above requirement &lt;code&gt;module&lt;/code&gt; can be a key player (best friend).&lt;/p&gt;

&lt;p&gt;There are &lt;strong&gt;six&lt;/strong&gt; (6) types of modules an application can have (depending on the requirement. Not necessary an application should have all the six)&lt;/p&gt;

&lt;p&gt;1️⃣ Root Module (🔴 Mandatory 🔴)&lt;br&gt;
2️⃣ Feature Module (🟡 Optional 🟡)&lt;br&gt;
3️⃣ Routing Module ( 🟡 Optional 🟡)&lt;br&gt;
4️⃣ Service Module (🟡 Optional 🟡)&lt;br&gt;
5️⃣ Widget Module (🟡 Optional 🟡)&lt;br&gt;
6️⃣ Shared Module (🟡 Optional 🟡)&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Root Module&lt;/strong&gt;&lt;br&gt;
I bet you have already seen a glimpse of this module.&lt;br&gt;
When you create an Angular application for the first time you are provided with a module &lt;code&gt;app.module.ts&lt;/code&gt; remember???&lt;br&gt;
This is the root module (you can think this as the parent, all other modules will be directly or indirectly part of this module.&lt;br&gt;
Only &lt;strong&gt;one&lt;/strong&gt; root module is allowed in an application.&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Feature Module&lt;/strong&gt;&lt;br&gt;
In a large application (say an eCommerce application like amazon) with multiple different features you need to break the application into smaller chunks depending on the features (like users, product, payment etc.)&lt;br&gt;
These smaller chunks can be grouped under modules known as the Feature module.&lt;br&gt;
As an analogy you can think like you have a parent folder called entertainment under that you have different subcategory or folders like music, movies, games, photos etc. This subfolders you can think as feature module. All related items are grouped under one module.&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Routing Module&lt;/strong&gt;&lt;br&gt;
When an application has the navigation feature (more than one screen to display) then the routing module comes into play. In this scenario the Routing module is &lt;strong&gt;required&lt;/strong&gt;. &lt;br&gt;
If you are interested to know more about routing module you can have a look at this &lt;a href="https://dev.to/anubhab5/setting-up-angular-routing-from-scratch-1i07"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;4️⃣ &lt;strong&gt;Service Module&lt;/strong&gt; - (I will talk in detail very soon)&lt;br&gt;
Modules that contain only services (can be utility services).&lt;br&gt;
The service module must be present only at the &lt;strong&gt;root module&lt;/strong&gt; .&lt;br&gt;
Once I talk about services this part would be more clearer to you.&lt;/p&gt;

&lt;p&gt;5️⃣ &lt;strong&gt;Widget Module&lt;/strong&gt;&lt;br&gt;
The third party UI component library module which exposes components/ pipes/ directives (in the export array) can be considered as widget module.&lt;br&gt;
For example you have built a table component and you want to share it with other teams in your org. The module which contains the table component can be considered as a widget module.&lt;br&gt;
The other team would be importing your module and use the table.&lt;/p&gt;

&lt;p&gt;6️⃣ &lt;strong&gt;Shared Module&lt;/strong&gt;&lt;br&gt;
The commonly used components/ directives/ pipes which are used across the application in different modules are put in a special module called the &lt;code&gt;shared module&lt;/code&gt;.&lt;br&gt;
Now consider you have made a table component. This table component need to be used in different screens of &lt;u&gt;your&lt;/u&gt; application across different modules. The only way you can do is by using the &lt;em&gt;Shared Module&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;Hope you enjoyed reading the post (only theory but this knowledge will be helpful in the coming days. Trust Me)&lt;/p&gt;

&lt;p&gt;If you liked it please do &lt;em&gt;like&lt;/em&gt; &lt;em&gt;share&lt;/em&gt; and &lt;em&gt;comment&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Coming up next &lt;em&gt;Communication between components&lt;/em&gt;.&lt;br&gt;
So stay tuned.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt;&lt;br&gt;
So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>typescript</category>
      <category>modules</category>
    </item>
    <item>
      <title>Creating Custom Module in Angular</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Thu, 13 Jan 2022 13:11:30 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/creating-custom-module-in-angular-4p66</link>
      <guid>https://forem.com/playfulprogramming-angular/creating-custom-module-in-angular-4p66</guid>
      <description>&lt;p&gt;Today we will learn how to create our own Angular Module. If you are not aware of Angular Modules I would recommend you to go through this &lt;a href="https://dev.to/this-is-angular/understanding-angular-modules-41pb"&gt;post&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;In order to create a module we will be using the below CLI command &lt;br&gt;
&lt;code&gt;ng generate module &amp;lt;module_name&amp;gt;&lt;/code&gt;&lt;br&gt;
or the shorthand&lt;br&gt;
&lt;code&gt;ng g m &amp;lt;module_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets open a command prompt from the project root and type in the below code -&lt;br&gt;
&lt;code&gt;ng g m payroll&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the command prompt you will see like below -&lt;br&gt;
&lt;a href="https://media2.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%2Foobmrhdu34gri8bk3jiu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foobmrhdu34gri8bk3jiu.PNG" alt=" " width="645" height="54"&gt;&lt;/a&gt;&lt;br&gt;
Lets see how the project looks like -&lt;br&gt;
&lt;a href="https://media2.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%2F58b2jzo1fzoqofvne2ln.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F58b2jzo1fzoqofvne2ln.PNG" alt=" " width="418" height="209"&gt;&lt;/a&gt;&lt;br&gt;
So here we can see a folder with the module name has been created and a file called &lt;code&gt;payroll.module.ts&lt;/code&gt; file has been created.&lt;/p&gt;

&lt;p&gt;Lets open the file and see -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class PayrollModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here in the above code we can see the similar structure as we saw in the module provided by Angular the &lt;code&gt;app.module.ts&lt;/code&gt; file.&lt;br&gt;
One more thing you have noticed the declaration array is empty as we dont have any component/ pipe/ directive associated with this module yet. If we want to put any of the component/ pipe/ directive inside this directive we need to add in this list.&lt;/p&gt;

&lt;p&gt;Lets see how to do that. For that lets open the prompt again (from the project root folder) and type in the CLI command to create a &lt;a href="https://dev.to/anubhab5/creating-angular-component-129e"&gt;component&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lets run the below command: &lt;code&gt;ng g c PayrollDashboard&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But wait ✋🏻, you might ask a question if I run the above command in which module my component will get added???&lt;br&gt;
In the above case still the new component will be added to the &lt;code&gt;app.module&lt;/code&gt;. &lt;br&gt;
To specify the module also we need to add some extra flag to the above 👆🏻 CLI command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g component PayrollDashboard &lt;br&gt;
--module=payroll/payroll.module.ts&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2F7jn8ob8keh4b2w8qpf28.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7jn8ob8keh4b2w8qpf28.PNG" alt=" " width="800" height="111"&gt;&lt;/a&gt;&lt;br&gt;
Here you can see that the very similar output we saw earlier when creating &lt;a href="https://dev.to/anubhab5/creating-angular-component-129e"&gt;custom component&lt;/a&gt;, But one minor difference is there the last line where you can see the &lt;strong&gt;UPDATE&lt;/strong&gt; has happened to the &lt;br&gt;
&lt;code&gt;payroll.module.ts&lt;/code&gt; file and &lt;strong&gt;not&lt;/strong&gt; app.module.ts&lt;/p&gt;

&lt;p&gt;Now if you open the payroll.module.ts file you will see that the declarations array has been added with the newly created component.&lt;br&gt;
But still you might not be so happy as the component has been created outside the payroll folder (marked with yellow arrow)-&lt;br&gt;
&lt;a href="https://media2.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%2F0rqh6b7w4pdwpuh55bwi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0rqh6b7w4pdwpuh55bwi.PNG" alt=" " width="418" height="256"&gt;&lt;/a&gt;&lt;br&gt;
If you see closely the payroll-dashboard was created outside the payroll folder. But I need all the payroll features under the payroll folder itself right?&lt;br&gt;
For that we need to do a minor tweak in the earlier CLI command used to create the component -&lt;br&gt;
&lt;code&gt;ng g component payroll/PayrollDashboard --module=payroll/payroll.module.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you see we just added the folder name before the Component name while creating the component. Cool right!!!&lt;/p&gt;

&lt;p&gt;So we added the Component. &lt;br&gt;
But how to use? &lt;br&gt;
Is it the same thing like earlier?&lt;br&gt;&lt;br&gt;
Take the component selector and add it in &lt;code&gt;app.component.html&lt;/code&gt; file like below?&lt;br&gt;
&lt;a href="https://media2.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%2Fmcgl7yf7co9r5ka34inr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmcgl7yf7co9r5ka34inr.PNG" alt=" " width="800" height="157"&gt;&lt;/a&gt;&lt;br&gt;
The answer is NO!!!&lt;br&gt;
2 more steps (I say VERY IMPORTANT steps you need to take care of)&lt;/p&gt;

&lt;p&gt;1️⃣ Since the payroll.component is part of a separate module (payroll module) you need to &lt;strong&gt;import&lt;/strong&gt; the payroll module in the module where you will be using (app.module). &lt;br&gt;
In this case we will be using the payroll.component (present in payroll module) inside &lt;code&gt;app.component.html&lt;/code&gt; file, which is present inside the &lt;code&gt;app.module.ts&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fajd64ftfvbju8955frvb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fajd64ftfvbju8955frvb.PNG" alt=" " width="800" height="713"&gt;&lt;/a&gt;&lt;br&gt;
See how we added the newly created module? &lt;br&gt;
In the imports array we added the name of the payroll.module. We discussed &lt;a href="https://dev.to/this-is-angular/understanding-angular-modules-41pb"&gt;earlier&lt;/a&gt; right?&lt;/p&gt;

&lt;p&gt;2️⃣ Just adding the module is not enough. We need to add the component (we are using outside) in the exports array of the module where it has been declared.&lt;br&gt;
So in our case, we need to add the name of the component to the &lt;code&gt;exports&lt;/code&gt; array of the &lt;code&gt;payroll.module&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1jpstotk7r49nma3hyln.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1jpstotk7r49nma3hyln.PNG" alt=" " width="666" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it...&lt;br&gt;
If you open the browser you must see the below output -&lt;br&gt;
&lt;a href="https://media2.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%2F4ax3i85ahfn6k4v3hsvu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4ax3i85ahfn6k4v3hsvu.PNG" alt=" " width="478" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the post.&lt;br&gt;
Coming up next the different types of modules we can have in Angular. Stay tuned!!!&lt;br&gt;
If you liked the post please do like share and comment.&lt;/p&gt;

&lt;p&gt;I will be &lt;a href="https://twitter.com/Anubhab_0905" rel="noopener noreferrer"&gt;tweeting&lt;/a&gt; more on &lt;code&gt;Angular&lt;/code&gt; &lt;code&gt;JavaScript&lt;/code&gt; &lt;code&gt;TypeScript&lt;/code&gt; &lt;code&gt;CSS&lt;/code&gt;&lt;br&gt;
So hope to see you there too 😃&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>typescript</category>
      <category>modules</category>
    </item>
    <item>
      <title>Understanding Angular Modules</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Mon, 10 Jan 2022 19:21:25 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/understanding-angular-modules-41pb</link>
      <guid>https://forem.com/playfulprogramming-angular/understanding-angular-modules-41pb</guid>
      <description>&lt;p&gt;Today we will understand one of the very important topic in Angular - &lt;strong&gt;Angular Modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In simple term a module is a logical block or container that holds something.&lt;/p&gt;

&lt;p&gt;If we need to describe module in terms of Angular we can say its a logical block containing &lt;a href="https://dev.to/anubhab5/understanding-angular-component-2bpa"&gt;components&lt;/a&gt;,&lt;br&gt;
&lt;a href="https://dev.to/this-is-angular/custom-directives-in-angular-2cp5"&gt;custom directives&lt;/a&gt;, &lt;a href="https://dev.to/this-is-angular/creating-custom-pipe-in-angular-2pam"&gt;custom pipes&lt;/a&gt; and services (I will talk in detail very soon).&lt;/p&gt;

&lt;p&gt;Once you create a new Angular project the Angular team provided you with a ready to use module called &lt;code&gt;app.module.ts&lt;/code&gt;. Lets see the different parts of &lt;code&gt;Angular Module&lt;/code&gt; -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now few key take away -&lt;br&gt;
1️⃣ A module is a simple typescript class. &lt;/p&gt;

&lt;p&gt;2️⃣ The module is decorated with &lt;code&gt;@NgModule&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;3️⃣ The NgModule decorator is a function that takes an object. This object is also called &lt;strong&gt;metadata object&lt;/strong&gt; (you should remember this term)&lt;/p&gt;

&lt;p&gt;4️⃣ The metadata has information/ details which helps Angular how to compile and launch the application.&lt;/p&gt;

&lt;p&gt;In the above example you can see only 4 options but in total there are 9 options available. So lets see each one of them and their use (few are advanced options which will be taken in details later).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;declarations&lt;/code&gt; - &lt;br&gt;
It is an Array of classes. &lt;br&gt;
Holds list of all the custom directives, pipes and components user has created.&lt;br&gt;
All the items (directive, pipe, component) you added in this list are all part of this module.&lt;br&gt;
&lt;strong&gt;Note --- A Very Important One&lt;/strong&gt;&lt;br&gt;
You Can add a component/ directive/ pipe class in only one module's declaration array. If you try to add in more than one place you will get a compile error.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;imports&lt;/code&gt; - &lt;br&gt;
It is a list of all modules that this current module is dependent on.&lt;br&gt;
In the above example you can see &lt;code&gt;BrowserModule&lt;/code&gt; added. It means the current module is dependent on the BrowserModule to function correctly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;providers&lt;/code&gt; – &lt;br&gt;
It contains the list of dependency injection providers. (Might be a bit hard to digest at this time so in easy term I can say list of services. I will revisit this part when we talk about services)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;exports&lt;/code&gt; - &lt;br&gt;
List of custom components, directives, pipes that the module exposes/ exports so that the other module can use it.&lt;br&gt;
Now try to relate with import... This module will be exporting and some other module will be importing. So the other module should write the name of this module in that module's import array. Easy right???&lt;/p&gt;

&lt;p&gt;&lt;code&gt;entryComponents&lt;/code&gt; - &lt;br&gt;
It is a list of components that should be compiled when this module is defined. &lt;br&gt;
An Angular app always has at least one entry component, root component- &lt;code&gt;AppComponent&lt;/code&gt; by default.&lt;br&gt;
We will learn about dynamic components later. All dynamic components need to be part of EntryComponent list.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bootstrap&lt;/code&gt; –&lt;br&gt;
List of components that are bootstrapped/ during the start and the listed components will be automatically added to entryComponents.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;schemas&lt;/code&gt; - &lt;br&gt;
List of elements and properties that are neither Angular components or directives (again will talk about it later. It will be discussed in Advanced topic part).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;id&lt;/code&gt; – &lt;br&gt;
The Id used to identify the modules in getModuleFactory. If left undefined, the NgModule will not be registered with getModuleFactory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jit&lt;/code&gt; -&lt;br&gt;
When this is present, the module will be ignored by the AOT compiler. (Again will be covered in the advanced part)&lt;/p&gt;

&lt;p&gt;The main use of module comes to logically group similar items and put all the related items together. &lt;/p&gt;

&lt;p&gt;Suppose you have a feature like dashboard. All the related components will be placed under that module.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed reading the post.&lt;br&gt;
Coming up next is &lt;strong&gt;Creating custom module in Angular&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you enjoyed reading the post please do like comment subscribe&lt;br&gt;
and share with your friends.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>module</category>
    </item>
    <item>
      <title>Creating Custom Pipe in Angular</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sun, 09 Jan 2022 20:01:43 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/creating-custom-pipe-in-angular-2pam</link>
      <guid>https://forem.com/playfulprogramming-angular/creating-custom-pipe-in-angular-2pam</guid>
      <description>&lt;p&gt;Today we will learn how to create our own Pipe or the Custom Pipe. If you are unaware about what a Pipe is in Angular then I would recommend you to go through the previous articles I wrote on Pipe. You can start from &lt;a href="https://dev.to/this-is-angular/built-in-angular-pipes-part-1-23ec"&gt;here&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The need of custom pipe comes when the built in pipe does not meet our requirement.&lt;/p&gt;

&lt;p&gt;✩ So now lets see how can we create our first Pipe ✩&lt;/p&gt;

&lt;p&gt;The CLI command to create a Pipe is -&lt;br&gt;
&lt;code&gt;ng generate pipe &amp;lt;pipe-name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;the shorthand&lt;/em&gt; -&lt;br&gt;
&lt;code&gt;ng g p &amp;lt;pipe-name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets open the command prompt in the project root and type in the below command -&lt;br&gt;
&lt;code&gt;ng g p custom-pipe-demo&lt;/code&gt;&lt;br&gt;
So here we are saying Angular to create a pipe with the name &lt;br&gt;
&lt;code&gt;custom-pipe-demo&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.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%2F22kromc05e2ncezpd0rq.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F22kromc05e2ncezpd0rq.PNG" alt=" " width="721" height="85"&gt;&lt;/a&gt;&lt;br&gt;
and you will see 2 files getting created in the project -&lt;br&gt;
&lt;a href="https://media2.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%2F8vzx99bd0fqg391s34cm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8vzx99bd0fqg391s34cm.PNG" alt=" " width="486" height="394"&gt;&lt;/a&gt;&lt;br&gt;
The file pointed with the red arrow is the spec file (for writing unit test). We will not talk about it right now.&lt;br&gt;
We are more interested in writing our first pipe and will work with the file pointed with yellow arrow (the second one).&lt;/p&gt;

&lt;p&gt;One more line we can see in the command prompt which tells that the &lt;code&gt;app.module.ts&lt;/code&gt; has been updated. &lt;br&gt;
If you open the app.module.ts file you will see a new line got added and its the name of our pipe.&lt;br&gt;
&lt;a href="https://media2.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%2F73hchtbli3u7bhw4uuqp.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F73hchtbli3u7bhw4uuqp.PNG" alt=" " width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
I will talk about modules in very details in the very next post.&lt;/p&gt;

&lt;p&gt;So lets see how the pipe looks like -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'customPipeDemo'
})
export class CustomPipeDemoPipe implements PipeTransform {
  transform(value: unknown, ...args: unknown[]): unknown {
    return null;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets note our observation points -&lt;br&gt;
1️⃣ There is a normal typescript class &lt;code&gt;CustomPipeDemoPipe&lt;/code&gt; &lt;br&gt;
2️⃣ The class implements an interface &lt;code&gt;PipeTransform&lt;/code&gt;&lt;br&gt;
3️⃣ The Class is marked with a &lt;code&gt;@Pipe&lt;/code&gt; decorator&lt;br&gt;
4️⃣ The Pipe Decorator takes an object, which has the name of the pipe&lt;br&gt;
5️⃣ The &lt;code&gt;PipeTransform&lt;/code&gt; interface has a method called &lt;code&gt;transform&lt;/code&gt; which we need to implement (🔴 its mandatory 🔴)&lt;br&gt;
6️⃣ The &lt;code&gt;transform&lt;/code&gt; method has few arguments. The signature is already provided but we need to tweak it as per our need. The first argument is the value which we need to transform and the remaining args is an array of all the pipe parameters. Remember when we used the date pipe we passed 'short'/ 'medium' etc as parameters.&lt;br&gt;
7️⃣ The type is unknown by default but we can change it.&lt;/p&gt;

&lt;p&gt;So now the next question what we are trying to build using the pipe. &lt;/p&gt;

&lt;p&gt;Say our project has a file picker. Once it picks a file from the system we need to show the file size in Mb by default and if we pass a unit like GB it will convert that accordingly.&lt;/p&gt;

&lt;p&gt;So lets paste the below code and modify the transform function -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  transform(fileSize: number, ...args: string[]): string {
    if (!args.length || args[0] === 'MB') {
      return (fileSize / (1024 * 1024)).toFixed(2) + 'MB';
    } else if (args[0] === 'KB') {
      return (fileSize / 1024).toFixed(2) + 'KB';
    } else {
      return (fileSize / (1024 * 1024 * 1024)).toFixed(2) + 'GB';
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in the &lt;code&gt;app.component.html file&lt;/code&gt; -&lt;br&gt;
&lt;a href="https://media2.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%2Fc3r0wnibo14bihc3x0ou.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fc3r0wnibo14bihc3x0ou.PNG" alt=" " width="478" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h3&amp;gt;Custom Pipe Demo&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ 2000405677 | customPipeDemo : 'KB' }}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand the code on a high level.&lt;br&gt;
The first parameter which will come to the transform function is the value that we need to transform/ alter. In this case &lt;code&gt;2000405677&lt;/code&gt;&lt;br&gt;
Then we are using the pipe operator (|) and then the name of the pipe &lt;code&gt;customPipeDemo&lt;/code&gt;&lt;br&gt;
Then we can pass an argument in this case the unit where we need to convert to in this case &lt;code&gt;KB&lt;/code&gt;&lt;br&gt;
You can pass multiple arguments also and will be received by the args array, in the transform function.&lt;/p&gt;

&lt;p&gt;In the output we will see -&lt;br&gt;
&lt;a href="https://media2.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%2Fws0fzkiqdzi6j8nl3e2t.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fws0fzkiqdzi6j8nl3e2t.PNG" alt=" " width="295" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets see what debugger tells us -&lt;br&gt;
&lt;a href="https://media2.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%2Fdy3nbpwcu5tf4ggksgl9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdy3nbpwcu5tf4ggksgl9.PNG" alt=" " width="702" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see the argument &lt;code&gt;fileSize&lt;/code&gt; receives the value &lt;br&gt;
&lt;strong&gt;2000405677&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;args[]&lt;/code&gt; receives &lt;strong&gt;KB&lt;/strong&gt; as the first item in the array which we were passing in the argument.&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
The most important thing is the return keyword.&lt;br&gt;
After we did the transformation you can see we need to return the transformed value else nothing will be displayed in the template/ browser.&lt;/p&gt;

&lt;p&gt;That's all for now my friend 👋🏼 &lt;/p&gt;

&lt;p&gt;Hope you enjoyed the reading the post.&lt;br&gt;
If yes do like comment and share.&lt;br&gt;
Coming up next is the Module in Angular. So stay tuned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>tutorials</category>
      <category>pipe</category>
    </item>
    <item>
      <title>Built-In Angular Pipes -Part 5</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sat, 08 Jan 2022 18:14:19 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-part-5-pco</link>
      <guid>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-part-5-pco</guid>
      <description>&lt;p&gt;&lt;strong&gt;T&lt;/strong&gt;oday we will continue to learn the built in Angular pipes.&lt;/p&gt;

&lt;p&gt;Pipes covered in this post -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LowerCasePipe&lt;/code&gt;&lt;br&gt;
&lt;code&gt;PercentPipe&lt;/code&gt;&lt;br&gt;
&lt;code&gt;SlicePipe&lt;/code&gt;&lt;br&gt;
&lt;code&gt;TitleCasePipe&lt;/code&gt;&lt;br&gt;
&lt;code&gt;UpperCasePPipe&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;code&gt;LowerCasePipe&lt;/code&gt;&lt;br&gt;
This pipe is used to convert the string of alphabets in to lower case or in small letters&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value | lowercase }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported&lt;/strong&gt; from the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It takes only &lt;code&gt;string&lt;/code&gt; as Input.&lt;/p&gt;

&lt;p&gt;Now lets see the example. Our playground 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhxeb9amwb2sbfxl2gdu7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhxeb9amwb2sbfxl2gdu7.PNG" alt=" " width="509" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets paste in the below code in the component.ts file-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myName = 'JOHN DOE';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the component html file lets paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;hr&amp;gt;
&amp;lt;h2&amp;gt;Lowercase Pipe Demo&amp;lt;/h2&amp;gt;
&amp;lt;h3&amp;gt;Without Pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{myName }}&amp;lt;/p&amp;gt;
&amp;lt;h3&amp;gt;With Pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{myName | lowercase}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the browser you will see the below output 👇&lt;br&gt;
&lt;a href="https://media2.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%2Ffkez6wn62vurw92jpwtu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffkez6wn62vurw92jpwtu.PNG" alt=" " width="332" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here you can see without using pipe we are getting the original value but after using the &lt;code&gt;lowercasePipe&lt;/code&gt; we are getting the value converted to &lt;strong&gt;lowercase&lt;/strong&gt;.&lt;/p&gt;



&lt;p&gt;&lt;code&gt;PercentPipe&lt;/code&gt;&lt;br&gt;
It modifies a number to a percentage string. It can be formatted according to locale rule that determines the size of the group and the separator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value_expression | percent [ : digitsInfo [ : locale ] ] }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported&lt;/strong&gt; from the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It takes only &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;number&lt;/code&gt; as Input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;digitsInfo&lt;/code&gt; - &lt;br&gt;
Its exactly the same as the digitsInfo in the &lt;code&gt;decimal pipe&lt;/code&gt;. I would recommend you to have a look &lt;a href="https://dev.to/this-is-angular/built-in-angular-pipes-decimalpipe-part-3-33p1"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets see in practice -&lt;br&gt;
We will be working in the same playground.&lt;br&gt;
In the component.ts file lets paste the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; percentValue = 0.234769;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; in the template file lets paste the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;Percent Pipe Demo&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;{{ percentValue | percent }} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets see the output -&lt;br&gt;
&lt;a href="https://media2.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%2F7yyr9qkdbwzb1ox49p7e.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7yyr9qkdbwzb1ox49p7e.PNG" alt=" " width="327" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see that the output is &lt;strong&gt;23%&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Things to watch&lt;/em&gt;&lt;br&gt;
The value got rounded and a percent sign has been appended.&lt;br&gt;
Now if you update the &lt;code&gt;percentValue&lt;/code&gt; variable to &lt;strong&gt;0.237769&lt;/strong&gt;&lt;br&gt;
you will see the output to become 24%&lt;br&gt;
If we do not pass the digits info in that case there is no digits after the decimal point and it will be rounded off.&lt;/p&gt;

&lt;p&gt;Now lets see how digits info works. Lets paste in the below code in the template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;hr&amp;gt;
&amp;lt;h3&amp;gt;digitsInfo Example&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ percentValue | percent: '2.2-3'}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the output will become -&lt;br&gt;
&lt;a href="https://media2.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%2Fj0v0d41okrwoqdkfiuwv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj0v0d41okrwoqdkfiuwv.PNG" alt=" " width="348" height="249"&gt;&lt;/a&gt;&lt;br&gt;
&lt;u&gt;Explanation of the above output&lt;/u&gt;&lt;br&gt;
Digit two (2) before the decimal signifies how many digits will be there before decimal point in the output.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
If the output value has 2 digits before decimal (for example in this case) but you give 1 before decimal in the digitsInfo (example &lt;br&gt;
&lt;code&gt;&amp;lt;p&amp;gt;{{ percentValue | percent: '1.2-3'}} &amp;lt;/p&amp;gt;&lt;/code&gt;) still you will see 2 digits before the decimal.&lt;br&gt;
If you want 3 digits before the decimal then the digitsInfo should look like - 3.2-3 and 0 would be appended at the beginning.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1.2-3&lt;/code&gt; =&amp;gt; 2 and 3 signifies the minimum and maximum digits after the decimal place. It means minimum 2 digits will be there and maximum 3. Since the value we passed has more than 3 digits so we can see 3 digits after the decimal place.&lt;/p&gt;



&lt;p&gt;&lt;code&gt;SlicePipe&lt;/code&gt;&lt;br&gt;
This pipe creates a subset of the input string or array of items.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value_expression | slice : start [ : end ] }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported&lt;/strong&gt; from the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It takes string or array as Input&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;start&lt;/code&gt;&lt;br&gt;
It is of type number&lt;br&gt;
It is mandatory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;end&lt;/code&gt;&lt;br&gt;
It is a number. Marks the end position of the string/ array (But end is not included).&lt;br&gt;
It is optional&lt;br&gt;
Default value is undefined.&lt;/p&gt;

&lt;p&gt;Now lets jump in to see the example.&lt;br&gt;
Lets add the below code in the component.ts file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  sliceDemoString = 'John Snow';

  sliceDemoArray = ['a', 'b', 'c', 'd', 'e'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; in the template file lets add the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;Slice Pipe Demo&amp;lt;/h2&amp;gt;
&amp;lt;hr&amp;gt;
&amp;lt;h3&amp;gt;Without using slice pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ sliceDemoString }} &amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;{{ sliceDemoArray }} &amp;lt;/p&amp;gt;
&amp;lt;h3&amp;gt;Without using slice pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ sliceDemoString | slice: 2 }} &amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;{{ sliceDemoArray | slice: 1}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the output will be like below - &lt;br&gt;
&lt;a href="https://media2.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%2Fxkrvfk18nd1w41ke0hxh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxkrvfk18nd1w41ke0hxh.PNG" alt=" " width="381" height="393"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
Here we have used the slice pipe and in the first example we passed 2 as the staring point. So from the string &lt;code&gt;John Snow&lt;/code&gt; the character &lt;strong&gt;Jo&lt;/strong&gt; was sliced out/ removed and the remaining string was returned (J is the 0th index and it started from 2nd index).&lt;br&gt;
In the second example we passed 1 so the first item or the 0th index item was removed and started from the 1st index.&lt;/p&gt;

&lt;p&gt;Lets update the above code and write the below code in the template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;{{ sliceDemoString | slice: 2:4 }} &amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;{{ sliceDemoArray | slice: 1:3}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; the output will become -&lt;br&gt;
&lt;a href="https://media2.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%2Fl2o33d8p87560jmcjtay.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl2o33d8p87560jmcjtay.PNG" alt=" " width="365" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here in the first example the subset starts from 2nd index and ended in (end - 1) index the 3rd index.&lt;br&gt;
In the second example the start index was 1 and end index was not 3 but (endIndex - 1) i.e. the 2nd index so b, c was returned.&lt;/p&gt;



&lt;p&gt;&lt;code&gt;TitleCasePipe&lt;/code&gt;&lt;br&gt;
This pipe transforms the text to the title case. That is if you pass a sentence then every first letter of the word passed would become in capital case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value_expression | titlecase }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported&lt;/strong&gt; from the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It takes a string as input.&lt;/p&gt;

&lt;p&gt;Lets see in example. Lets add the below code in the component.ts file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;titleCasePipeDemoString = 'You will be master in Angular 
very soon!';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;TitleCase pipe&amp;lt;/h2&amp;gt;
&amp;lt;h3&amp;gt;Without using pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ titleCasePipeDemoString }}&amp;lt;/p&amp;gt;

&amp;lt;h3&amp;gt;With using the Titlecase pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt;{{ titleCasePipeDemoString | titlecase}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will become -&lt;br&gt;
&lt;a href="https://media2.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%2Ffxuleukfsh0zzn15eidi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffxuleukfsh0zzn15eidi.PNG" alt=" " width="364" height="296"&gt;&lt;/a&gt;&lt;br&gt;
So here you can see every first letter of the word is capitalized.&lt;/p&gt;



&lt;p&gt;The last pipe...........&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UpperCasePipe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Its the opposite of &lt;code&gt;LowerCasePipe&lt;/code&gt;. Converts the string into uppercase. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value_expression | uppercase }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported&lt;/strong&gt; from the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It takes a string as input.&lt;/p&gt;

&lt;p&gt;Lets see in example. Lets add the below code in the component.ts file -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;uppsercasePipeDemo = 'john snow';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the template file -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;UpperCase Pipe Demo&amp;lt;/h2&amp;gt;
&amp;lt;h3&amp;gt;Without Pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt; {{ uppsercasePipeDemo }}&amp;lt;/p&amp;gt;

&amp;lt;h3&amp;gt;With Pipe&amp;lt;/h3&amp;gt;
&amp;lt;p&amp;gt; {{ uppsercasePipeDemo | uppercase }}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will become -&lt;br&gt;
&lt;a href="https://media2.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%2F5ckfif8g43nwy9al3xlm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5ckfif8g43nwy9al3xlm.PNG" alt=" " width="341" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It transforms the string we passed to uppercase.&lt;/p&gt;

&lt;p&gt;That's all my friend. You have done a great job covering all the pipes (few I left intentionally will cover very soon).&lt;br&gt;
Next coming up &lt;strong&gt;Creating custom pipe&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the post.&lt;br&gt;
If yes do like comment and share. More Angular topics are on the way. Stay tuned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>tutorials</category>
      <category>beginners</category>
      <category>pipe</category>
    </item>
    <item>
      <title>Built-In Angular Pipes -Part 4</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Sat, 08 Jan 2022 14:37:05 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-part-4-3cg3</link>
      <guid>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-part-4-3cg3</guid>
      <description>&lt;p&gt;&lt;strong&gt;T&lt;/strong&gt;oday we will continue to learn the built in Angular pipes.&lt;br&gt;
Pipes covered in this post -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JsonPipe&lt;/code&gt;&lt;br&gt;
&lt;code&gt;KeyValuePipe&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;code&gt;JsonPipe&lt;/code&gt;&lt;br&gt;
This pipe is used to format a data into JSON-format representation.&lt;br&gt;
Mostly I use for debugging purpose.&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported from&lt;/strong&gt; the &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ value_expression | json }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Its a simple pipe.&lt;br&gt;
Lets see the example. Our project folder -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fodjgrmvusqitx5w3gxxa.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fodjgrmvusqitx5w3gxxa.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
In the component.ts file lets write the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  jsonPipeData = {
    studentName: "John Doe",
    studentMarks: 423
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; in the html lets paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;JSON Pipe&amp;lt;/h2&amp;gt;
&amp;lt;h4&amp;gt;Without the pipe&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;{{jsonPipeData}}&amp;lt;/p&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;h4&amp;gt;With the pipe&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;{{jsonPipeData | json}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, lets see the output -&lt;br&gt;
&lt;a href="https://media2.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%2Fuaqkxk7cunb0eodqcpdf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuaqkxk7cunb0eodqcpdf.PNG" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see without the pipe the output is &lt;code&gt;[object Object]&lt;/code&gt;&lt;br&gt;
It is not able to process the code. But after using the pipe we can view the correct data. &lt;br&gt;
So, you can use this pipe to verify the data coming in the component by displaying directly in the template.&lt;/p&gt;



&lt;p&gt;&lt;code&gt;KeyValuePipe&lt;/code&gt;&lt;br&gt;
This pipe is used to convert an Object or Map into an array of key value pair.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;{{ input_expression | keyvalue [ : compareFn ] }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is &lt;strong&gt;Exported from&lt;/strong&gt; &lt;code&gt;CommonModule&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;compareFn&lt;/code&gt;&lt;br&gt;
If you want to provide a custom sorting function.&lt;br&gt;
It is Optional&lt;br&gt;
Default is the &lt;code&gt;defaultComparator&lt;/code&gt; which Angular provides to sort.&lt;/p&gt;

&lt;p&gt;Now lets see an example -&lt;br&gt;
So in the component.ts file lets add the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  obj = {
    'chair': '23',
    'banana': '3',
    'apple': '4',
  };


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

&lt;/div&gt;



&lt;p&gt;Please note that we have put the first key as &lt;code&gt;chair&lt;/code&gt; and &lt;code&gt;apple&lt;/code&gt; at the end.&lt;/p&gt;

&lt;p&gt;Now lets paste the below code in the template file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;hr&amp;gt;

&amp;lt;h2&amp;gt;KeyValue Pipe&amp;lt;/h2&amp;gt;
{{ obj | keyvalue | json }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would see the below output -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fiso7q2ojgp3zolpq1395.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fiso7q2ojgp3zolpq1395.PNG" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here few important points to observe - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have added one more pipe the &lt;code&gt;json pipe&lt;/code&gt; along with the &lt;code&gt;keyvalue&lt;/code&gt;. So we can chain multiple pipe at the same time. Output of the first pipe acts as the input to the second.&lt;/li&gt;
&lt;li&gt;In the output you can see the key &lt;code&gt;apple&lt;/code&gt; came at the beginning (it got sorted) while &lt;code&gt;chair&lt;/code&gt; at the end although the value provided was opposite.&lt;/li&gt;
&lt;li&gt;We received an array of key-value objects.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
We already saw a glimpse of sorting. The keyValue pipe use the &lt;code&gt;defaultComparator&lt;/code&gt; to sort the output values.&lt;br&gt;
Following ae the key points to note-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the key is a number then it will sort in Ascending Order.&lt;/li&gt;
&lt;li&gt;If key is string then it will sort in alphabetic order.&lt;/li&gt;
&lt;li&gt;If key is of different type then it will get converted to string &lt;/li&gt;
&lt;li&gt;If key is null or undefined then it will be put at the very end.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Custom Sorting&lt;/strong&gt; Values using &lt;code&gt;compareFn&lt;/code&gt;&lt;br&gt;
Now lets write a custom function which will sort the list on its values. So paste in the below code in the component.ts file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; orderbyValueAsc = (a: KeyValue&amp;lt;string, string&amp;gt;, b:
 KeyValue&amp;lt;string, string&amp;gt;): number =&amp;gt; {
    return Number(a.value) &amp;lt; Number(b.value) ? -1 : 
(Number(a.value) &amp;gt; Number(b.value)) ? 0 : 1
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp; in the template file we need to pass the compare function also -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ obj | keyvalue: orderbyValueAsc | json }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we write &lt;code&gt;: orderbyValueAsc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output in the browser you will see -&lt;br&gt;
&lt;a href="https://media2.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%2Fs5g5xhl0lt5fmn52x1t6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fs5g5xhl0lt5fmn52x1t6.PNG" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There can be a scenario where you want to keep the original value. In that case we can write the below function in the component ts file -&lt;br&gt;
&lt;code&gt;keepOriginal(a: any, b: any) {&lt;br&gt;
    return a;&lt;br&gt;
  }&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's all for now. I will be discussing on the remaining pipes in the coming posts. So stay tuned.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the post.&lt;br&gt;
If yes do like share and comment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;u&gt;Happy Coding&lt;/u&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Built-In Angular Pipes - DecimalPipe - Part 3</title>
      <dc:creator>Anubhab Mukherjee</dc:creator>
      <pubDate>Wed, 05 Jan 2022 17:15:40 +0000</pubDate>
      <link>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-decimalpipe-part-3-33p1</link>
      <guid>https://forem.com/playfulprogramming-angular/built-in-angular-pipes-decimalpipe-part-3-33p1</guid>
      <description>&lt;p&gt;&lt;strong&gt;T&lt;/strong&gt;oday we will continue with the remaining built in Angular pipes. If you are not aware of pipe I would recommend you to go through the &lt;a href="https://dev.to/this-is-angular/built-in-angular-pipes-part-1-23ec"&gt;post&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;DecimalPipe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The DecimalPipe is used to format a value/ number as per the required decimal digits and locale information.&lt;/p&gt;

&lt;p&gt;The name of the pipe is &lt;code&gt;number&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;{{ value | number [ : digitsInfo [ : locale ] ] }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Present in the &lt;code&gt;Common Module&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Input value
&lt;/h3&gt;

&lt;p&gt;Input Value which the pipe accepts must be either in &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;number&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Parameter
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;digitsInfo&lt;/code&gt;&lt;br&gt;
It is of type &lt;code&gt;string&lt;/code&gt;.&lt;br&gt;
It is used to set the digit and decimal representation.&lt;br&gt;
It is Optional.&lt;br&gt;
Default value is undefined.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locale&lt;/code&gt;&lt;br&gt;
It is of type string.&lt;br&gt;
It specifies what locale format that will be implemented.&lt;br&gt;
It is Optional.&lt;br&gt;
Default value is undefined.&lt;/p&gt;

&lt;p&gt;The digitsInfo follows the following format -&lt;br&gt;
&lt;code&gt;{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;minIntegerDigits&lt;/code&gt;&lt;br&gt;
Minimum number of integer digits before the decimal point.&lt;br&gt;
Default value is 1.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;minFractionDigits&lt;/code&gt;&lt;br&gt;
Minimum number of digits allowed after the decimal point.&lt;br&gt;
Default is 0.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;maxFractionDigits&lt;/code&gt;&lt;br&gt;
Maximum number of digits allowed after the decimal point.&lt;br&gt;
Default is 3.&lt;/p&gt;

&lt;p&gt;Now, lets see in practice. Lets open the component.ts file -&lt;br&gt;
&lt;a href="https://media2.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%2Fr6h986m6wqhn9dkew7sc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fr6h986m6wqhn9dkew7sc.PNG" alt=" " width="509" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And add a variable &lt;code&gt;pi&lt;/code&gt; and assign the value &lt;u&gt;3.14159&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  pi = 3.14159;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in the corresponding template file lets add the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h2&amp;gt;Decimal Pipe&amp;lt;/h2&amp;gt;

&amp;lt;h4&amp;gt;Without the pipe&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;{{ pi }}&amp;lt;/p&amp;gt;
&amp;lt;hr /&amp;gt;
&amp;lt;h4&amp;gt;Default Decimal Pipe&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;{{ pi | number }}&amp;lt;/p&amp;gt;
&amp;lt;hr /&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;We will see the below output -&lt;br&gt;
&lt;a href="https://media2.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%2Fg7997prub6pd8jbze3d2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg7997prub6pd8jbze3d2.PNG" alt=" " width="236" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here in the above output we can see when we display the value of &lt;code&gt;pi&lt;/code&gt; without any pipe it shows the entire value. But when we use the decimal pipe the number of digits after decimal becomes &lt;strong&gt;3&lt;/strong&gt; and does the rounding off too.&lt;/p&gt;

&lt;p&gt;Now lets see the &lt;code&gt;digitsInfo&lt;/code&gt; parameter in detail-&lt;/p&gt;

&lt;p&gt;Lets paste in the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h4&amp;gt;digitsInfo Example&amp;lt;/h4&amp;gt;

&amp;lt;p&amp;gt;
  Here number of digits before decimal is 1. &amp;lt;br&amp;gt;
  Minimum number of digits after decimal is 1 &amp;lt;br&amp;gt;
  Maximum numberof digits after decimal is 2 &amp;lt;br&amp;gt;
  &amp;lt;i&amp;gt;Output- &amp;lt;/i&amp;gt;
  &amp;lt;b&amp;gt;{{ pi | number: "1.1-2" }}&amp;lt;/b&amp;gt;
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
  Here number of digits before decimal is 3. 
Since the value has only one digit so the remaining 
digits are covered by 0.&amp;lt;br&amp;gt;
  Minimum number of digits after decimal is 2. &amp;lt;br&amp;gt;
  Maximum numberof digits after decimal is 4. 
Number of digits shown after decimal is 4. &amp;lt;br&amp;gt;
  &amp;lt;i&amp;gt;Output- &amp;lt;/i&amp;gt;
  &amp;lt;b&amp;gt;{{ pi | number: "3.2-4" }}&amp;lt;/b&amp;gt;
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
  No digits after the Decimal Point. &amp;lt;br&amp;gt;
  &amp;lt;i&amp;gt;Output- &amp;lt;/i&amp;gt;
  &amp;lt;b&amp;gt;{{ pi | number: "1.0-0" }}&amp;lt;/b&amp;gt;
&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the output for the above code you would see - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fj44wxh4qjcp273mtbelh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj44wxh4qjcp273mtbelh.PNG" alt=" " width="766" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all for now. &lt;br&gt;
Coming up the remaining pipes in the next post.&lt;br&gt;
So stay tuned...&lt;br&gt;
Hope you enjoyed the post if yes do like share and comment!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers&lt;/strong&gt;!!!&lt;br&gt;
&lt;em&gt;Happy Coding&lt;/em&gt;&lt;/p&gt;

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