<?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: sadegh</title>
    <description>The latest articles on Forem by sadegh (@sadeghsourcecode).</description>
    <link>https://forem.com/sadeghsourcecode</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%2F1163135%2F73508dd8-6b63-4d67-816e-dc227a414820.png</url>
      <title>Forem: sadegh</title>
      <link>https://forem.com/sadeghsourcecode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sadeghsourcecode"/>
    <language>en</language>
    <item>
      <title>Angular Pipe | Starter point</title>
      <dc:creator>sadegh</dc:creator>
      <pubDate>Fri, 20 Oct 2023 08:25:15 +0000</pubDate>
      <link>https://forem.com/sadeghsourcecode/angular-pipe-starter-point-4k2</link>
      <guid>https://forem.com/sadeghsourcecode/angular-pipe-starter-point-4k2</guid>
      <description>&lt;h2&gt;
  
  
  Hello, everyone
&lt;/h2&gt;

&lt;p&gt;Today we're going to talk about &lt;a href="https://angular.io/guide/pipes"&gt;&lt;strong&gt;Pipes&lt;/strong&gt;&lt;/a&gt; in the &lt;a href="https://angular.io"&gt;&lt;strong&gt;Angular framework&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Before we continue, it's better to get a handle on this Framework. If you're working with this Framework, this article is meant for you to use the &lt;strong&gt;Pipe&lt;/strong&gt; to achieve &lt;strong&gt;good performance&lt;/strong&gt; for various operations that need to alter data.&lt;/p&gt;

&lt;p&gt;The primary question here is: &lt;/p&gt;

&lt;h2&gt;
  
  
  When should we use Pipe?
&lt;/h2&gt;

&lt;p&gt;To respond to this question, first we have to see what the problem is.&lt;/p&gt;

&lt;p&gt;Let's set a simple example. &lt;br&gt;
Suppose we want to display a user's name and last name in our template. This seems to be an easy task, right? &lt;br&gt;
To do this, all we need to do is fetch the relevant data from the appropriate API and display it.&lt;/p&gt;



&lt;p&gt;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;user = {
    name: 'john',
    lastName: 'Doe'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;html 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; {{user.name}} {{user.lastName}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We also have the &lt;strong&gt;date&lt;/strong&gt; along with the user's name and last name coming from the API. We want to display the date too. &lt;br&gt;
It seems simple. &lt;br&gt;
All we have to do is use the binding method and put the value in a tag like &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;. &lt;/p&gt;



&lt;p&gt;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;user = {
    name: 'john',
    lastName: 'Doe'
    date: new Date()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;html 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; {{user.name}} {{user.lastName}} &amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt; {{user.date}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Let's see the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu83coc8jim4796dwfqtr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu83coc8jim4796dwfqtr.png" alt="Display data in template" width="597" height="181"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;As you can see in the image above, everything is working fine. However, the main issue is displaying date. &lt;br&gt;
The format of this date is not standard and we have to display it in a format that is understandable to the user.&lt;/p&gt;

&lt;p&gt;As you know, we can call a function inside the template. &lt;br&gt;
It is beyond awesome. 😎&lt;br&gt;
Thanks, Angular. 😍&lt;br&gt;
So let's write the function we need.&lt;/p&gt;



&lt;p&gt;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;formatDate(date: Date): string {
    let result: string;
    result = date.getUTCFullYear().toString() + '/' + 
    date.getUTCMonth().toString() + '/' + 
    date.getUTCDate().toString();
    return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;html 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; {{formatDate(user.date)}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;In the function we received the date we needed and converted it to a readable date for humans.&lt;br&gt;
Now, let's look at the output again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ugge9svpf4y3xm2y7e7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ugge9svpf4y3xm2y7e7.png" alt="Function to format date" width="250" height="147"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Allright. Task done! &lt;/p&gt;

&lt;p&gt;Everything goes well until we don't have any change detection in our template. &lt;br&gt;
In simple words, until Angular re-renders a part of the template. Let's suppose that every seconds we have something to do automatically and a data change occurs in our template.&lt;br&gt;
For example, let's put a timer that updates the value every second and displays it in the template.&lt;/p&gt;



&lt;p&gt;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;counter: number = 0;

ngOnInit(): void {
  setInterval(() =&amp;gt; {
    this.counter++;
  }, 1000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;html 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}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;The output we want is something like the below picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nkane38ryrr5rzj3pvg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nkane38ryrr5rzj3pvg.gif" alt="Display timer" width="380" height="182"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Ok, every thing works well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need a Pipe?
&lt;/h2&gt;

&lt;p&gt;The reason for using Pipe is that this process of changing date only happens once and until there is new data, it won't change again. &lt;br&gt;
&lt;strong&gt;What does it mean?&lt;/strong&gt; &lt;br&gt;
It means that, if we put a &lt;code&gt;log&lt;/code&gt; inside the function which changes the &lt;strong&gt;date&lt;/strong&gt;, every time the counter changes, the function will also be called.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsk5bhudltk7p8e6hzuzw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsk5bhudltk7p8e6hzuzw.gif" alt="console.log for funcction that used in template" width="1044" height="946"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Now, suppose that instead of having one data, we have one hundred data. It will create a big mess. To solve this problem, we should use Pipe.&lt;/p&gt;

&lt;p&gt;Pipe ensures us that for every data, it will only be executed once and if any change is not detected, it won't be executed again. This way we can improve the &lt;strong&gt;performance&lt;/strong&gt;. &lt;br&gt;
Let's move this function to a Pipe file.&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({
  standalone: true,
  name: 'formatDate'
})

export class FormatDatePipe implements PipeTransform {

  transform(date: Date): string {
    let result: string;
    result = date.getUTCFullYear().toString() + '/' + 
    date.getUTCMonth().toString() + '/' + 
    date.getUTCDate().toString();
    return result;
  }

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

&lt;/div&gt;






&lt;p&gt;A Pipe is a class which implements PipeTransform. Inside it the &lt;code&gt;transform()&lt;/code&gt; function is used, in which we can receive different values and apply our changes on them and at the end will result in having our desired output.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use?
&lt;/h2&gt;

&lt;p&gt;Just import this Pipe in imports array of any module that you want to use it in. And after that you can access the Pipe using its name and actually invoke it in the template!&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  imports: [
    FormatDatePipe
  ],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;And then you can use it in yout template like this:&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; {{user.date | formatDate}} &amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We write the value we need, we use the pipe markup ( &lt;code&gt;|&lt;/code&gt; ) and finally we write the name of the pipe we need.&lt;/p&gt;

&lt;p&gt;If you want to have more values in the pipe just add a &lt;code&gt;:&lt;/code&gt; after the pipe name and add more values. You have a third value too? No problem, add a &lt;code&gt;:&lt;/code&gt; again and add your third value. But as you already know it's better to have less inputs for a function or use a model for sending your data.&lt;/p&gt;

&lt;p&gt;If we put  a &lt;code&gt;log&lt;/code&gt; statement here you can notice the changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ylh700d6qlq0vktwau8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ylh700d6qlq0vktwau8.gif" alt="console.log for pipe" width="846" height="782"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Let's recap. In this article, we saw how a disaster would occur if we used a function inside a template. Whenever Angular detects a change (doesn't matter to which part of the template), the functions that are being used in the template are executed once and this is harmful for a complex page.&lt;/p&gt;

&lt;p&gt;The solution to this is using a &lt;strong&gt;Pipe&lt;/strong&gt; which allows us to change our data but only executes our function (the data change function) once. It automatically gives us the result with only receiving the input on the changed variable and shows no reaction otherwise.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article. I hope it has been helpful to you.&lt;/p&gt;

&lt;p&gt;If you have any questions, you can ask them right here.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>frontend</category>
      <category>web</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
