<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: devashishSarmah</title>
    <description>The latest articles on DEV Community by devashishSarmah (@devashishsarmah).</description>
    <link>https://dev.to/devashishsarmah</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%2F722289%2Fcda93b74-b3a6-4d30-aaaa-5e3522c06e61.jpg</url>
      <title>DEV Community: devashishSarmah</title>
      <link>https://dev.to/devashishsarmah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devashishsarmah"/>
    <language>en</language>
    <item>
      <title>A Dive Into Angular Pipes</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Sun, 28 Aug 2022 16:45:00 +0000</pubDate>
      <link>https://dev.to/playfulprogramming-angular/a-dive-into-angular-pipes-1eg4</link>
      <guid>https://dev.to/playfulprogramming-angular/a-dive-into-angular-pipes-1eg4</guid>
      <description>&lt;p&gt;Pipes are simple functions to use in &lt;a href="https://angular.io/guide/glossary#template-expression" rel="noopener noreferrer"&gt;template expressions&lt;/a&gt; to accept an input value and return a transformed value.&lt;/p&gt;

&lt;p&gt;For Example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, the pipe firstName will take a fullName as an input and transform it into a First Name. It can be used both in a html and ts file as shown below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can check out some of the &lt;a href="https://angular.io/guide/pipes-overview#built-in-pipes" rel="noopener noreferrer"&gt;built-in pipes&lt;/a&gt; that Angular provides.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple Parameters in a pipe
&lt;/h3&gt;

&lt;p&gt;A pipe can have multiple parameters. Let’s create a standalone pipe that will construct a string based on the given parameters.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;gt; ng g pipe user-string --standalone --flat --skip-tests&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, userString pipe requires three parameters provided to it. The parameters needs to be provided in the template expression as shown above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lifecycle hooks in a pipe
&lt;/h3&gt;

&lt;p&gt;An Angular pipe only supports OnDestroy lifecycle hook. You can clear out any subscription or data that you might use inside the pipe. Just to make sure there is no memory leaks after the pipe’s instance gets destroyed. &lt;br&gt;
For reference, you can check out the implementation of the &lt;a href="https://github.com/angular/angular/blob/main/packages/common/src/pipes/async_pipe.ts" rel="noopener noreferrer"&gt;async pipe&lt;/a&gt;. It uses the OnDestroy hook to unsubscribe any subscription.&lt;/p&gt;
&lt;h3&gt;
  
  
  Pure and Impure Pipes
&lt;/h3&gt;

&lt;p&gt;By default, pipes are defined as &lt;em&gt;pure&lt;/em&gt; so that Angular executes the pipe only when it detects a &lt;em&gt;pure change&lt;/em&gt; to the input value. &lt;br&gt;
Pure pipes are memoized, this is why the pipe’s transform method only gets called when any of its input parameter is changed.&lt;/p&gt;

&lt;p&gt;So, why does impure pipes exist?&lt;/p&gt;

&lt;p&gt;Let’s say you have an array of users which gets processed by the pipe and returns a filtered version of the users.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now, the users will get filtered on the first run. What if you append a new user to the array? The pure pipe won’t detect the change as the user array itself hasn’t changed.&lt;/p&gt;

&lt;p&gt;One of the solution for this is creating a brand new instance of the array whenever a new user gets added. i.e &lt;code&gt;users = [...users, newUser]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or, you can use an impure pipe&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;An Impure Pipe is not memoized. It gets evaluated on every change detection cycle irrespective of its input value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While an impure pipe can be useful, be careful using one. A long-running impure pipe could dramatically slow down your application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can implement your own memoization procedure to optimize the impure pipe.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on &lt;a href="https://medium.com/@devashish.dev.sarmah" rel="noopener noreferrer"&gt;Medium&lt;/a&gt; or &lt;a href="https://dev.to/devashishsarmah"&gt;Dev&lt;/a&gt; to read more about Angular&lt;/em&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Custom Structural Directives in Angular</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Mon, 06 Jun 2022 18:02:26 +0000</pubDate>
      <link>https://dev.to/devashishsarmah/custom-structural-directives-in-angular-a8i</link>
      <guid>https://dev.to/devashishsarmah/custom-structural-directives-in-angular-a8i</guid>
      <description>&lt;h2&gt;
  
  
  Custom Structural Directives in Angular
&lt;/h2&gt;

&lt;p&gt;In Angular, Structural directives are the directives that can manipulate the DOM. There are a few built-in structural directives that developers uses in almost every angular applications such as *ngIf, *ngFor, *ngTemplateOutlet, etc.&lt;/p&gt;

&lt;p&gt;It’s really amazing how the built-in directives works underneath.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ngFor Structural Directive
&lt;/h3&gt;

&lt;p&gt;The ngFor structural directive takes a iterable collection and renders a template each iteration. The template is the element that the directive is applied on.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yQrbZ8SV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2180/1%2A9_PWeSgrfpIUnMp5qUPHxQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yQrbZ8SV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2180/1%2A9_PWeSgrfpIUnMp5qUPHxQ.png" alt="*ngFor example" width="880" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, debts is a collection with values such as&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cwxt6t3f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3024/1%2ARJNSUckf5GvX1rAm4x4JlA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cwxt6t3f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3024/1%2ARJNSUckf5GvX1rAm4x4JlA.png" alt="debt collection" width="880" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For each of the items in debts the element li will get rendered.&lt;/p&gt;

&lt;p&gt;The output for the above code is&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1Ty__kpc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AVVpfpP4R1cieEaAb_wn3wQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1Ty__kpc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AVVpfpP4R1cieEaAb_wn3wQ.png" alt="Output intial" width="320" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, What happens underneath the compilation of ngFor?&lt;/p&gt;

&lt;p&gt;A structural directive encloses the element inside a template fragment and the template fragment into a viewContainer.&lt;/p&gt;

&lt;p&gt;The statement&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rHvCqbbw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2176/1%2AtlUaY7V6hRev81iC528ICg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rHvCqbbw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2176/1%2AtlUaY7V6hRev81iC528ICg.png" alt="ngFor debts statement" width="880" height="40"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;gets compiled into this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3aA44csR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A9b1pq8q8DVvQZRr6yNjxzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3aA44csR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A9b1pq8q8DVvQZRr6yNjxzg.png" alt="ngFor debt compiled statement" width="738" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple terms, The ngFor directive gets the collection and renders the templates with specifying their each context. &lt;br&gt;
Each context contains index, $implicit(the particular value of the collection item) and a few other values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s modify the ngFor directive
&lt;/h3&gt;

&lt;p&gt;Let’s extend the ngFor directive and develope another feature which is filtering.&lt;/p&gt;

&lt;p&gt;We want to do something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gsMAXEDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ATewCXEhfewPJFmEBcJ7vRw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gsMAXEDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ATewCXEhfewPJFmEBcJ7vRw.png" alt="filter feature in the directive" width="526" height="38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where myFilter is&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4zXJT-Wj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AKpFBY0bfoJMLVJlJM4XI3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4zXJT-Wj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AKpFBY0bfoJMLVJlJM4XI3w.png" alt="myFilter method" width="714" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to create a directive called ngForIf&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Specifying it’s selector&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IPvCPhWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Aexq3IXZbHKbaQtQUgGbLaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IPvCPhWG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Aexq3IXZbHKbaQtQUgGbLaw.png" alt="directive selector" width="574" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let’s extend the existing NgFor directive&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--znXN5ydO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2800/1%2AIzNjGVd3AGOrPjFZAXH9WA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--znXN5ydO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2800/1%2AIzNjGVd3AGOrPjFZAXH9WA.png" alt="ngForOfIf member" width="880" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We need to create our own alternative of ngForOf member. Where we’ll store the iterable collection and use it for filtering. As the _ngForOf member from the NgFor class is private, We can only set a value to it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FrmtKhWo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnCZ4C9op6Y4GQytzV2gc8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FrmtKhWo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnCZ4C9op6Y4GQytzV2gc8g.png" alt="_ngFor alternative member" width="782" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Creating the setter for it&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--euO4egeL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AlV91zUkfumHg4iQp0F2q2Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--euO4egeL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AlV91zUkfumHg4iQp0F2q2Q.png" alt="_ngForIfOf setter" width="880" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Implementing our ngDoCheck lifecycle hook and calling the ngDoCheck of ngFor class&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4Lv2DELz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AjICehm2cJ9dLemD3EE1pEA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4Lv2DELz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AjICehm2cJ9dLemD3EE1pEA.png" alt="ngDoCheck lifecycle hook" width="392" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now, To implement the filtering feature, we will create a predicate type for it&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yhAZDQCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AshSJXq5VwXh7S8ItvSLidg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yhAZDQCP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AshSJXq5VwXh7S8ItvSLidg.png" alt="predicate type for filter" width="880" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Member for storing the filter method&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nj5agEYU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A2SV_XuqRmjQMBLdnr3tl4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nj5agEYU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A2SV_XuqRmjQMBLdnr3tl4w.png" alt="filter member variable" width="676" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally the setter for the filter. Here, we are checking the availability for _ngForIfOf reference and assigning the filtered values to the ngFor setter of NgFor class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6GmRkcbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3000/1%2Az9GKQumxDrj4X9UC4NxddQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6GmRkcbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3000/1%2Az9GKQumxDrj4X9UC4NxddQ.png" alt="filter setter" width="880" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don’t forget to provide the instances to the super contructor&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8IRHbUFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2764/1%2A1XrTZD46QrPTzECKny9r5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8IRHbUFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2764/1%2A1XrTZD46QrPTzECKny9r5w.png" alt="constructor" width="880" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can use our custom structural directive with a filter like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O8qsm4rZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2768/1%2Ad2JLie1xZG-uj6z9mmmbwQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O8qsm4rZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2768/1%2Ad2JLie1xZG-uj6z9mmmbwQ.png" alt="template file with custom directive" width="880" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output using the ngForIf Directive&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DFoGz_15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AC7TqCiWZ8U0YAR3dZvOGnQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DFoGz_15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AC7TqCiWZ8U0YAR3dZvOGnQ.png" alt="Output after filtering feature" width="322" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the source code at given link: &lt;a href="https://stackblitz.com/edit/angular-ivy-vwlxuy"&gt;https://stackblitz.com/edit/angular-ivy-uoqa3h&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Directives in Angular</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Mon, 02 May 2022 06:46:36 +0000</pubDate>
      <link>https://dev.to/devashishsarmah/directives-in-angular-508a</link>
      <guid>https://dev.to/devashishsarmah/directives-in-angular-508a</guid>
      <description>&lt;h2&gt;
  
  
  Directives in Angular
&lt;/h2&gt;

&lt;p&gt;Directives in angular are kind of utilities that adds additional behaviour to elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  There are three types of directives
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://angular.io/guide/component-overview"&gt;Components&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Attribute Directives&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Structural Directives&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Components as Directives
&lt;/h3&gt;

&lt;p&gt;Every Angular component contains a selector. By using the selector you can include it in other components. A component is a kind of directive which also has a template attached to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a_IO-Gyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AsQVOb6DOqpD1AWH8XbcHsw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a_IO-Gyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AsQVOb6DOqpD1AWH8XbcHsw.png" alt="Angular component" width="752" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1qldRmlQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ABJVc1nSzZ8fYOeAy8a0jdQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1qldRmlQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ABJVc1nSzZ8fYOeAy8a0jdQ.png" alt="Including a component using a selector" width="748" height="74"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Attribute Directives
&lt;/h3&gt;

&lt;p&gt;Attribute directives modify the behaviour of other HTML elements, attributes, properties, and components. It can also listen to changes in its host.&lt;/p&gt;

&lt;p&gt;You might have come across some built-in attribute directives such as ngClass, ngStyle, ngModel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building an attribute directive
&lt;/h3&gt;

&lt;p&gt;Using Angular schematics, We can generate an Angular directive by executing&lt;/p&gt;

&lt;p&gt;ng generate directive make-bold&lt;/p&gt;

&lt;p&gt;The directive needs to be added to the declarations in the module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UbiCjeZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2An8qGcr5dJRUiHm4_7lHAzQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UbiCjeZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2An8qGcr5dJRUiHm4_7lHAzQ.png" alt="App module" width="880" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, Let’s use the directive. We will use the directive in a paragraph to make the text bold. The way we do it is providing the selector of the directive as a attribute in the element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xteh8rVc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AYz3Bm4xC19wWqDN-W5mA3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xteh8rVc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AYz3Bm4xC19wWqDN-W5mA3g.png" alt="Paragraph tag with the directive" width="868" height="34"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To make the text bold, We need to access the paragraph element and set it’s style. We will access the element using ElementRef from @angular/core.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F1cIwc1c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2040/1%2ARL9iZXo2BgUFkq6pOSnrpA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F1cIwc1c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2040/1%2ARL9iZXo2BgUFkq6pOSnrpA.png" alt="Access the element and setting it’s style" width="880" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Listening to the host element
&lt;/h3&gt;

&lt;p&gt;We can listen to the host element’s state using the HostListener decorator provided from @angular/core.&lt;/p&gt;

&lt;p&gt;Let’s build a directive name ‘hover-border’ that styles an element whenever it is hovered.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Offcourse we can do all these just using CSS classes but to demonstrate the possibilities of an Angular directive, we are styling using the directive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zNAs1JBE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2332/1%2AHJqFxEezBIr3GG3ZLvB8MA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zNAs1JBE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2332/1%2AHJqFxEezBIr3GG3ZLvB8MA.png" alt="Hover border directive" width="880" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The hover directive will look something like this. We have used the HostListener to listen to ‘mouseenter’ and ‘mouseleave’ events of the host and change it’s style accordingly.&lt;/p&gt;

&lt;p&gt;The host element will look something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tAxfSss9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2016/1%2AVUcLsNNUGGBzv3flTZZzTg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tAxfSss9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2016/1%2AVUcLsNNUGGBzv3flTZZzTg.png" alt="host-element" width="880" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The end result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rf4MndkZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AJCB690aixIzUHLlKGdhAcw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rf4MndkZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AJCB690aixIzUHLlKGdhAcw.gif" alt="Result of make-bold and hover-border directive" width="297" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing values into Directives
&lt;/h3&gt;

&lt;p&gt;We can pass values from the host element to the directive. We will use the Input Decorator provided from @angular/core.&lt;/p&gt;

&lt;p&gt;Let’s say we want to pass the border color to the hover-border directive. We are gonna create a Input variable name ‘appHoverBorder’ ( same name as the directive selector) and set a default value ‘black’.&lt;/p&gt;

&lt;p&gt;We are going to modify the mouseenter procedure to show the border color provided into the directive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4PHJkb3X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AwyWztBKcv3F_ZeIk__z55w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4PHJkb3X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AwyWztBKcv3F_ZeIk__z55w.png" alt="app-hover" width="658" height="38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZLavs5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2712/1%2AAy2SdoEQdFo1W3wPSlxIug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZLavs5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2712/1%2AAy2SdoEQdFo1W3wPSlxIug.png" alt="app-hover" width="880" height="25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The motive of setting the name of the variable same as the directive selector is to provide the border color to the directive itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qK5YLbI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2316/1%2AWw1okm85voS0bNfXGBdSzA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qK5YLbI5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2316/1%2AWw1okm85voS0bNfXGBdSzA.png" alt="directive" width="880" height="29"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, If we want to another configuration to the directive. Let’s say the border width. We can create another input variable named ‘borderWidth’ in the hover-border directive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ml7X4Vuy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AqIbimwDqm91qAyrKpp9Krg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ml7X4Vuy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AqIbimwDqm91qAyrKpp9Krg.png" alt="directive" width="542" height="34"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W89BqnN1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3240/1%2AyamJWAkSH58972fFTddpCQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W89BqnN1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3240/1%2AyamJWAkSH58972fFTddpCQ.png" alt="directive" width="880" height="20"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MNy4PfPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AkdeaM2qBDGh0VLGHGdqtXQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MNy4PfPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AkdeaM2qBDGh0VLGHGdqtXQ.png" alt="directive" width="880" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The purpose of creating another variable just for the borderWidth is just demonstrating how to have multiple Input variables in a directive. &lt;br&gt;
Ideally I would create a config variable for the directive. Let’s say { borderColor: string, borderWidth: string}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using HostBinding to change change host element’s properties
&lt;/h3&gt;

&lt;p&gt;We can bind a particular property of the host to a directive’s variable and change the value directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t2smKvvn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2APz0wBqI3o5yUXhJ16jVOWw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t2smKvvn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2APz0wBqI3o5yUXhJ16jVOWw.png" alt="Host binding" width="804" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QDCZg2zC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AOhqitFySYFVN_qLNGOgWHg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QDCZg2zC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AOhqitFySYFVN_qLNGOgWHg.png" alt="directive" width="460" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Structural Directives
&lt;/h3&gt;

&lt;p&gt;Structural directives are the directives that can manipulate the DOM Structure.&lt;/p&gt;

&lt;p&gt;Angular provides a few built-in structural directives that you may have already come across.&lt;/p&gt;

&lt;p&gt;NgIf, NgForOf, NgSwitch etc. See more in &lt;a href="https://angular.io/guide/built-in-directives"&gt;Built-in directives&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Building a custom structural directive
&lt;/h3&gt;

&lt;p&gt;We are going to create a structural directive which will receive a ‘userRole’ and a set of ‘Roles’. If ‘userRole’ consists in the List of ‘Roles’ then we will show the Element on which we have applied the directive.&lt;/p&gt;

&lt;p&gt;Let’s name the directive as ‘if-roles’&lt;/p&gt;

&lt;p&gt;ng generate directive if-roles&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZHkg7MGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2368/1%2AUjwDl5iKce4uumCjrGdHcQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZHkg7MGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2368/1%2AUjwDl5iKce4uumCjrGdHcQ.png" alt="if-role directive" width="880" height="870"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The usage in an element:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tXN_eRvI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2192/1%2ALKW394ma9Q9KzYpxGGdlLw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tXN_eRvI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2192/1%2ALKW394ma9Q9KzYpxGGdlLw.png" alt="directive" width="880" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If-role Directive:&lt;/p&gt;

&lt;p&gt;We are injecting TemplateRef and ViewContainerRef in the contructor.&lt;/p&gt;

&lt;p&gt;templateRef is the element that you have applied the directive to.&lt;/p&gt;

&lt;p&gt;viewContainerRef is the container for the element.&lt;/p&gt;

&lt;p&gt;So, when you apply a structural directive into an element. A viewContainer get’s created which has to ability to render the element(templateRef) within it’s container.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What about the * before the directive?&lt;br&gt;
 The asterisk, *, syntax on a structural directive, such as *&lt;a href="https://angular.io/api/common/NgIf"&gt;ngIf&lt;/a&gt;, is shorthand that Angular interprets into a longer form. Angular transforms the asterisk in front of a structural directive into an &lt;a href="https://angular.io/api/core/ng-template"&gt;&lt;/a&gt; that surrounds the host element and its descendants.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you might noticed that you can only specify one value/expression into the directive *directive=”value”&lt;/p&gt;

&lt;p&gt;How do we provide some extra context to the directive such as ‘Roles’ in these case?&lt;/p&gt;

&lt;p&gt;We are gonna create another Input whose name will be appIfRolesValues&lt;/p&gt;

&lt;p&gt;For any secondary Inputs the pattern for their name has to be&lt;/p&gt;

&lt;p&gt;{directive}{inputName}&lt;/p&gt;

&lt;p&gt;The way we provide the secondary input to the directive is by using this pattern&lt;/p&gt;

&lt;p&gt;*directive=”value;inputName”&lt;/p&gt;

&lt;p&gt;In our example, we did&lt;/p&gt;

&lt;p&gt;*appIfRoles=”’angularDev’; values: [‘angularDev’, ‘webDev’]”&lt;/p&gt;

&lt;p&gt;There are a few advanced topics in structural directives such as&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angular.io/guide/structural-directives#structural-directive-syntax-reference"&gt;Structural directive syntax&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angular.io/guide/structural-directives#improving-template-type-checking-for-custom-directives"&gt;Improving template type checking for custom directives&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will be covering these in separate articles.&lt;/p&gt;

&lt;p&gt;You can find the source code in given link: &lt;a href="https://stackblitz.com/edit/angular-ivy-uoqa3h"&gt;https://stackblitz.com/edit/angular-ivy-uoqa3h&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this article helped you to get started with Angular directives. If you have any queries or doubts, feel free to reach out to me in the comment box.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building a dialog(Material) service in angular</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Wed, 16 Mar 2022 16:29:05 +0000</pubDate>
      <link>https://dev.to/devashishsarmah/building-a-dialogmaterial-service-in-angular-2m07</link>
      <guid>https://dev.to/devashishsarmah/building-a-dialogmaterial-service-in-angular-2m07</guid>
      <description>&lt;p&gt;In angular development, the typical way we open a dialog is by injecting MatDialog directly into a component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fj9ghob5ogju5h30bcfs3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fj9ghob5ogju5h30bcfs3.png" alt="MatDialog injected into a component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqji0ro6x1s63tot66ns3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqji0ro6x1s63tot66ns3.png" alt="First scenario diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This implementation will be okay for a dialog that will be opened by a single component. &lt;br&gt;
But in the case of a kind of dialog that will be used by many components that belongs to different modules in the application, This approach won't work.&lt;/p&gt;

&lt;p&gt;One of the solutions can be a dialog service whose responsibilities will be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Take the inputs from the component(Which invokes the opening of the dialog).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Opens the dialog and passes the inputs to the dialog or stores the inputs as its member variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Returns the dialogRef to the component(Which invokes the opening of the dialog).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fb704649rlwq8631yy7k6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fb704649rlwq8631yy7k6.png" alt="Dialog service based solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A dialog module needs to be created which will hold the dialog Component and other modules that are required in the dialog component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The components that require to open the dialog will need to import the dialog module first.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fecrc08bj33d6rxe1zwns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fecrc08bj33d6rxe1zwns.png" alt="Dialog Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Dialog Module&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F11enzt369q4x06ki6j4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F11enzt369q4x06ki6j4u.png" alt="Dialog Module"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Importing the dialog module to the dedicated module that the component which will open the dialog resides in (In my case the app module)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fanox23pldcae7ymzq4pu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fanox23pldcae7ymzq4pu.png" alt="App Module"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The implementation of the open procedure in a component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F0gn3f5e6mkd69kly062j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F0gn3f5e6mkd69kly062j.png" alt="App component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also send out the data through the method parameter that we have defined in the service for the DialogConfig.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ffsusljlnxkc4fnnfvcn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ffsusljlnxkc4fnnfvcn3.png" alt="Another way to send data to dialog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entire source code is available on: &lt;a href="https://github.com/devashishSarmah/dialog-service" rel="noopener noreferrer"&gt;https://github.com/devashishSarmah/dialog-service&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for checking out. &lt;br&gt;
Please feel free to comment for any queries&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>programming</category>
      <category>material</category>
    </item>
    <item>
      <title>Standalone Components, directives and pipes in Angular</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Mon, 20 Dec 2021 18:16:42 +0000</pubDate>
      <link>https://dev.to/devashishsarmah/standalone-components-directives-and-pipes-in-angular-4a79</link>
      <guid>https://dev.to/devashishsarmah/standalone-components-directives-and-pipes-in-angular-4a79</guid>
      <description>&lt;p&gt;As of you who works with Angular knows that the Angular framework is revolving around the concept of &lt;code&gt;ngModule&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NgModule&lt;/code&gt; is currently one of the core concepts in Angular. Developers new to Angular need to learn about this concept before creating even a simple application.&lt;/p&gt;

&lt;p&gt;Given this central role of &lt;code&gt;NgModule&lt;/code&gt; in Angular it is hard to reason about components, directives and pipes in isolation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NgModules&lt;/code&gt; are the smallest reusable building blocks in Angular, not components.&lt;/p&gt;

&lt;h2&gt;
  
  
  The current scenario
&lt;/h2&gt;

&lt;p&gt;In today's Angular, whenever we create a component, we need to declare it in a ngModule. Irrespective of its use in other modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({...})
export class UserViewComponent {
  constructor(readonly service: UserViewService) {}
}

@NgModule({
 declarations: [UserViewComponent],
  imports: [/* dependencies here */],
  providers: [{provide: UserViewService, useClass: BackendUserViewService}],
})
export class UserViewModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What are the things a standalone component will offer?
&lt;/h2&gt;

&lt;p&gt;A standalone component will basically offer us to create components without creating a ngModule. &lt;/p&gt;

&lt;p&gt;It will look something like this.&lt;br&gt;
&lt;/p&gt;

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

@Component({
  standalone: true,  
  template: `I'm a standalone component!`
})
export class HelloStandaloneComponent {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a bunch of exciting ideas opens up with this concept coming into the picture &lt;/p&gt;

&lt;h2&gt;
  
  
  When will this feature get released?
&lt;/h2&gt;

&lt;p&gt;Recently the RFC for this feature has been completed&lt;br&gt;
&lt;a href="https://github.com/angular/angular/discussions/43784"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, We can expect it to get released soon&lt;/p&gt;

&lt;p&gt;You can also checkout this prototype &lt;a href="https://stackblitz.com/edit/ng-standalone"&gt;link&lt;/a&gt; to get an early look of how it's going to be.&lt;/p&gt;

&lt;p&gt;This feature is surely going to be a game changer&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks for Reading!
&lt;/h2&gt;

&lt;p&gt;Thanks for reading, I hope you enjoyed it!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Server side events using nodeJS</title>
      <dc:creator>devashishSarmah</dc:creator>
      <pubDate>Sun, 12 Dec 2021 15:27:37 +0000</pubDate>
      <link>https://dev.to/devashishsarmah/server-side-events-using-nodejs-4j53</link>
      <guid>https://dev.to/devashishsarmah/server-side-events-using-nodejs-4j53</guid>
      <description>&lt;p&gt;Server-Sent Events (SSE) is a technology based on HTTP. On the client-side, it provides an API called EventSource (part of the HTML5 standard) that allows us to connect to the server and receive updates from it.&lt;/p&gt;

&lt;p&gt;Let's create a simple chat system&lt;/p&gt;

&lt;p&gt;First, we are gonna create a new nodeJS project&lt;/p&gt;

&lt;p&gt;Let's name it &lt;strong&gt;sse-server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's initiate npm to install some dependencies&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Exi4eqy7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e9b1bzu4k3twc155klt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Exi4eqy7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e9b1bzu4k3twc155klt.png" alt="Initiate NPM" width="880" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are gonna add &lt;code&gt;express&lt;/code&gt;, &lt;code&gt;body-parser&lt;/code&gt; and &lt;code&gt;cors&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JkbSJyfX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9o6tabfduz2o6y2ynv4k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JkbSJyfX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9o6tabfduz2o6y2ynv4k.png" alt="NPM Install command" width="880" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;index.ts&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure the middleware
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';

const app = express();

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Specify the PORT
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PORT = 3000;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Declare and initialise the required variables
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Contains the connected clients info
let clients: any[] = []; 
// Contains the messages of the network
const messages: any[] = [];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a endpoint where the clients will subscribe to events
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/messages', (request: any, response: any) =&amp;gt; {
  const headers: any = {
    'Content-Type': 'text/event-stream',
    Connection: 'keep-alive',
    'Cache-Control': 'no-cache'
  };

  response.writeHead(200, headers);

  const data: string = `data: ${JSON.stringify(messages)}\n\n`;

  response.write(data);

  const clientId = (request.query.id ?? 'guest') + '_' + Date.now();

  const newClient: any = {
    id: clientId,
    response
  };

  clients.push(newClient);

  request.on('close', () =&amp;gt; {
    console.log(`${clientId} Connection closed`);
    clients = clients.filter((client: any) =&amp;gt; client.id !== clientId);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A function to notify whenever a new message arrives in the network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const notifyMessageToAllUsers = (userIdWithoutUnderscore: string) =&amp;gt; {
  clients
    .filter(
      (client: any) =&amp;gt;
        String(client.id).split('_')[0] != userIdWithoutUnderscore
    )
    .forEach((client: any) =&amp;gt; {
      client.response.write(`data: ${JSON.stringify(messages)}\n\n`);
    });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally an endpoint to push messages to the network
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/message', (request, response) =&amp;gt; {
  if (
    request.query.id &amp;amp;&amp;amp;
    request.body &amp;amp;&amp;amp;
    request.body.message &amp;amp;&amp;amp;
    String(request.body.message).trim()
  ) {
    messages.push({
      user_id: request.query.id,
      message: request.body.message.trim()
    });
    notifyMessageToAllUsers(String(request.query.id));
    response.send({ status: 200, error: 'Message Sent Succesfully' });
  } else {
    response.send({ status: 400, error: 'Bad Request' });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;let's start the server
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(PORT, () =&amp;gt; {
  console.log(`Server is listening at http://localhost:${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Server console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ Server is listening at http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For pushing a message to the network&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl --location --request POST 'http://localhost:3000/message?id=1234' \
--header 'Content-Type: application/json' \
--data-raw '{
    "message": "Hi"
}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To listen to the messages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --location --request GET 'http://localhost:3000/messages' \
--header 'Accept: text/event-stream'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After subscribing to the endpoint, we should be able to see the message that was pushed earlier&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ data: [{"user_id":"1234","message":"Hi"}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;SSE is very powerful in terms of receiving &lt;strong&gt;events&lt;/strong&gt; from the server to achieve real-time behaviour in the client. But it can get very expensive if it's not implemented efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks for Reading!
&lt;/h2&gt;

&lt;p&gt;Thanks for reading, I hope you enjoyed it!&lt;/p&gt;

</description>
      <category>node</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
