<?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: Manoj Prasanna  🚀</title>
    <description>The latest articles on DEV Community by Manoj Prasanna  🚀 (@mana95).</description>
    <link>https://dev.to/mana95</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%2F472799%2F78096696-7f46-4e6c-a697-433688e4e4ac.jpg</url>
      <title>DEV Community: Manoj Prasanna  🚀</title>
      <link>https://dev.to/mana95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mana95"/>
    <language>en</language>
    <item>
      <title>Angular: Micro Frontend (Module Federation)</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Fri, 26 Jul 2024 17:49:12 +0000</pubDate>
      <link>https://dev.to/mana95/angular-micro-frontend-module-federation-384o</link>
      <guid>https://dev.to/mana95/angular-micro-frontend-module-federation-384o</guid>
      <description>&lt;p&gt;In this article, we will explain the micro frontend architecture in Angular and how to build it in an application.&lt;/p&gt;

&lt;p&gt;In modern web development, the concept of micro frontends is gaining significant traction. Just as microservices revolutionized backend architecture, micro frontends bring similar benefits to the front end. This article explores what micro frontends are, their benefits, and how they can be implemented using Angular.&lt;br&gt;
First Let's talk about the micro-Frontend&lt;/p&gt;
&lt;h2&gt;
  
  
  What's a micro-frontend?
&lt;/h2&gt;

&lt;p&gt;Micro frontends are an architectural style where a frontend application is decomposed into smaller, semi-independent modules. Each module, or micro frontend, is &lt;strong&gt;developed&lt;/strong&gt;, &lt;strong&gt;tested&lt;/strong&gt;, and &lt;strong&gt;deployed&lt;/strong&gt; separately, often by different teams. This modular approach allows for more flexibility, scalability, and maintainability.&lt;br&gt;
Module Federation allows applications to load and share modules and their dependencies at runtime. This helps apps share code and resources, which reduces duplication and improves performance and development.&lt;/p&gt;

&lt;p&gt;Now that you have a basic understanding of micro frontends, let's explore the types of applications that implement this architecture.&lt;/p&gt;
&lt;h2&gt;
  
  
  Where do we can use micro-frontend?
&lt;/h2&gt;

&lt;p&gt;Micro frontend architecture can be implemented in various types of applications, especially those that benefit from scalability, flexibility, and independent deployment. such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;E-commerce Platforms:&lt;br&gt;
Sites like Amazon or eBay, where different teams manage different parts of the site (e.g., product pages, search functionality, user profiles, and checkout processes).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Social Media Networks:&lt;br&gt;
Platforms like Facebook or LinkedIn, where different features like news feeds, messaging, and notifications are handled by separate teams.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Implementing Micro Frontends with Angular
&lt;/h2&gt;

&lt;p&gt;There are several methods to implement a micro-frontend application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Iframe-Based: &lt;br&gt;
Simple but limited in terms of interaction and performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web Components:&lt;br&gt;
Custom elements that are encapsulated and reusable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build-Time Integration:&lt;br&gt;
Combining micro frontends during the build process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run-Time Integration: &lt;br&gt;
Loading and integrating micro frontends at runtime, often using module federation in Webpack.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a step-by-step explanation of how you can implement Micro Frontends in Angular using Module Federation&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Set up the Main Application Shell
&lt;/h2&gt;

&lt;p&gt;Create an Angular project using the Angular CLI&lt;br&gt;
&lt;code&gt;ng new shell-app --routing&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Configure Webpack Module Federation
&lt;/h2&gt;

&lt;p&gt;install the dependencies&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @angular-architects/module-federation --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add Module Federation configuration to webpack.config.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
  output: {
    publicPath: "http://localhost:4200/",
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "shell",
      remotes: {
        mfe1: "mfe1@http://localhost:4201/remoteEntry.js",
      },
      shared: ["@angular/core", "@angular/common", "@angular/router"],
    }),
  ],
};

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Setting Up Micro Frontend Applications
&lt;/h2&gt;

&lt;p&gt;Each micro frontend is an Angular application.&lt;br&gt;
Initialize Micro Frontend Project:&lt;br&gt;
&lt;code&gt;ng new mfe1 --routing&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configure Webpack Module Federation&lt;/strong&gt;&lt;br&gt;
Add Module Federation configuration to webpack.config.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
  output: {
    publicPath: "http://localhost:4201/",
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "mfe1",
      filename: "remoteEntry.js",
      exposes: {
        './Module': './src/app/feature/feature.module.ts',
      },
      shared: ["@angular/core", "@angular/common", "@angular/router"],
    }),
  ],
};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expose Angular Modules&lt;/strong&gt;&lt;br&gt;
Expose the necessary modules in your micro frontend's webpack.config.js and ensure they are correctly set up to be consumed by the shell application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Integrate Micro Frontends into Shell
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Load Remote Modules&lt;/strong&gt;&lt;br&gt;
Modify &lt;strong&gt;app-routing.module.ts&lt;/strong&gt; in the shell application to load remote modules&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  {
    path: 'mfe1',
    loadChildren: () =&amp;gt; import('mfe1/Module').then(m =&amp;gt; m.FeatureModule)
  }
];

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bootstrap Applications&lt;/strong&gt;&lt;br&gt;
Ensure both the shell and micro frontend applications are served and accessible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Running and Testing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Serve Applications&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;ng serve --project shell-app&lt;br&gt;
ng serve --project mfe1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Access the Shell Application&lt;/strong&gt;&lt;br&gt;
Navigate to &lt;a href="http://localhost:4200" rel="noopener noreferrer"&gt;http://localhost:4200&lt;/a&gt; and verify that the micro frontend is integrated correctly.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Micro frontends with Angular provide a robust solution to manage large-scale, complex web applications. By breaking down a monolithic frontend into smaller, manageable pieces, teams can work more efficiently and deploy independently. While the architecture introduces some complexities, the benefits in terms of scalability, flexibility, and maintainability often outweigh the challenges. By carefully planning and implementing micro frontends, organizations can achieve a more resilient and scalable frontend architecture.&lt;/p&gt;

&lt;p&gt;Thanks for reading the whole story ❤&lt;/p&gt;

</description>
      <category>angular</category>
      <category>frontend</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Angular Change Detection and OnPush Strategy 🚀</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Tue, 15 Aug 2023 18:17:28 +0000</pubDate>
      <link>https://dev.to/mana95/angular-change-detection-and-onpush-strategy-4l63</link>
      <guid>https://dev.to/mana95/angular-change-detection-and-onpush-strategy-4l63</guid>
      <description>&lt;p&gt;In this article, I will delve into the concept of Change Detection and the functionality of the &lt;strong&gt;OnPush&lt;/strong&gt; strategy within the Angular framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Angular Change Detection
&lt;/h2&gt;

&lt;p&gt;Have you ever encountered a scenario where your Angular application required automatic updates upon detecting the necessity for a change? If that's the case, you likely implemented Angular's Change Detection mechanism, specifically the &lt;strong&gt;OnPush&lt;/strong&gt; strategy. This strategy proves to be highly efficient in swiftly detecting changes, particularly for value type.Let jump to understand about this change detections.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While this approach ensures accuracy, it can lead to performance bottlenecks in complex applications&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you make any changes to your model in the application, Anulgar will &lt;a href="https://angular.io/api/core/ChangeDetectionStrategy"&gt;detect the changes&lt;/a&gt; and immediately update the view.Angular's change detection mechanism ensures continuous synchronization (&lt;a href="https://www.tutorialsteacher.com/angular/two-way-data-binding"&gt;Two way data binding&lt;/a&gt;) between the underlying models and their respective views.&lt;br&gt;
Let know about in angular can change as a result of any of the following sceniors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DOM Event&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AJAX HTTP requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Timers(SetInterval() , SetTimer())&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Understaning about the Angular change  detaction cycle
&lt;/h2&gt;

&lt;p&gt;Angular's change detection cycle is a critical part of how the framework updates and synchronizes the DOM with your application's data. It's responsible for detecting and propagating changes from your application's data model to the user interface. Understanding the change detection cycle is important for optimizing your application's performance and avoiding unnecessary updates.&lt;/p&gt;

&lt;p&gt;Suppose we have a simple Angular component called &lt;em&gt;devCounterComponent&lt;/em&gt; that displays a counter value and a button to increment it&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({
  selector: 'app-dev-counter',
  template: `
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Counter: {{ counter }}&amp;lt;/p&amp;gt;
      &amp;lt;button (click)="increment()"&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  `,
})
export class DevCounterComponent {
  counter = 0;

  increment() : void {
    this.counter++;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's follow the change detection  cycle with above example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Initialization:&lt;br&gt;
When the application starts, Angular initializes the DevCounterComponent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rendering Initial View:&lt;br&gt;
The initial view is rendered, showing the counter value as 0 and the "Increment" button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event Handling and Data Binding:&lt;br&gt;
When the user clicks the "Increment" button, the increment() method is triggered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Change Detection Triggers:&lt;br&gt;
The increment() method changes the value of the counter property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Change Detection Process:&lt;br&gt;
Angular's change detection system detects the change in the counter property and starts the change detection process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking Components for Changes:&lt;br&gt;
Angular checks the &lt;em&gt;DevCounterComponent&lt;/em&gt; for changes. It compares the new value of the counter property (1) with the previous value (0).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating the DOM:&lt;br&gt;
Since there is a change in the counter property, Angular updates the DOM to reflect the new value. The counter value displayed in the template is now 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Child Components:&lt;br&gt;
In this example, there are no child components, so this step is skipped.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat Process: &lt;br&gt;
The change detection cycle is complete for now. If another change is triggered (e.g., another button click), the process will repeat.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's explore a more complex example involving parent and child components. Suppose we have a &lt;strong&gt;ParentComponent&lt;/strong&gt; that contains two instances of the &lt;strong&gt;DevCounterComponent&lt;/strong&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 { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    &amp;lt;div&amp;gt;
      &amp;lt;app-dev-counter&amp;gt;&amp;lt;/app-dev-counter&amp;gt;
      &amp;lt;app-dev-counter&amp;gt;&amp;lt;/app-dev-counter&amp;gt;
    &amp;lt;/div&amp;gt;
  `,
})
export class ParentComponent {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;But we should still be mindful of performance considerations and use strategies like &lt;strong&gt;OnPush&lt;/strong&gt; change detection when appropriate to optimize their applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Certainly, let's delve into the &lt;strong&gt;OnPush&lt;/strong&gt; change detection strategy in Angular. This strategy is a significant aspect of the change detection cycle, and understanding it is essential for effective application development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding about &lt;em&gt;OnPush&lt;/em&gt; Detection Strategy
&lt;/h2&gt;

&lt;p&gt;The "OnPush" change detection strategy is a feature in Angular that optimizes the change detection process by reducing the number of checks and updates made by Angular's change detection mechanism. It is designed to improve the &lt;em&gt;performance&lt;/em&gt; of your Angular applications by focusing on components that explicitly indicate when their input properties have changed.&lt;br&gt;
By default, Angular uses the "Default" change detection strategy, where it checks for changes in all components and their templates on every change detection cycle. This can lead to unnecessary checks and updates, impacting performance in larger applications. The "&lt;strong&gt;OnPush&lt;/strong&gt;" strategy provides a way to mitigate this by making components more selective about when they should be checked for changes.&lt;/p&gt;

&lt;p&gt;Here is a table summarizing the key differences between the two strategies&lt;br&gt;
&lt;strong&gt;Default&lt;/strong&gt;      : Every change detection cycle&lt;br&gt;
&lt;strong&gt;OnPush&lt;/strong&gt;       : When inputs or properties change&lt;/p&gt;
&lt;h2&gt;
  
  
  When to use OnPush
&lt;/h2&gt;

&lt;p&gt;The "&lt;strong&gt;OnPush&lt;/strong&gt;" change detection strategy should be used for components that are not constantly changing. This includes components that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Display static data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only receive data from their parent components&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do not perform any calculations or transformations on their data&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To set the "&lt;strong&gt;OnPush&lt;/strong&gt;" strategy for a component, you specify it in the component's metadata using the changeDetection property. Here's an example&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, Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';

@Component({
  selector: 'app-onpush-dev',
  templateUrl: './onpush-dev.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class OnPushDevComponent {
  @Input() data: any;

  constructor(private cdr: ChangeDetectorRef) {}

  // This method marks the component for change detection.
  triggerChangeDetection() {
    this.cdr.markForCheck();
  }

  // Event handler that updates the input property.
  updateData() {
    this.data = { newValue: 'Updated value' };
  }
}

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

&lt;/div&gt;



&lt;p&gt;In this example, the component &lt;em&gt;OnPushdevComponent&lt;/em&gt; uses the "OnPush" change detection strategy. It has an input property data and a method triggerChangeDetection() that can be used to manually trigger change detection.&lt;/p&gt;

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

&lt;p&gt;The &lt;strong&gt;OnPush&lt;/strong&gt; change detection strategy can be a powerful tool for improving the performance of your Angular applications. However, it is important to use it wisely and to make sure that your components are using immutable data structures. By following these guidelines, you can use the &lt;strong&gt;OnPush&lt;/strong&gt; change detection strategy to optimize your Angular applications and improve their performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That’s It!&lt;/strong&gt;&lt;br&gt;
Thanks for reading the whole story ❤&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>angular</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Use RXJS Debounce Time With Angular</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Tue, 01 Aug 2023 04:46:07 +0000</pubDate>
      <link>https://dev.to/mana95/how-to-use-rxjs-debounce-time-with-angular-4aj5</link>
      <guid>https://dev.to/mana95/how-to-use-rxjs-debounce-time-with-angular-4aj5</guid>
      <description>&lt;p&gt;In this article, I will guide you through the concept of debounce time and how to use it in your Angular applications with &lt;strong&gt;RxJS&lt;/strong&gt; operators. Debounce time helps efficiently manage user input and optimize application performance. I'll provide a step-by-step guide to help you understand how to use debounce time and improve your application's performance and usability. First, let's explore what debounce time is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Debounce Time?
&lt;/h2&gt;

&lt;p&gt;Debounce time is a technique used to prevent an event from being triggered too frequently. This can be especially useful in situations where you want to avoid the user spamming a button or sending an excessive number of requests to a server. For instance, let's consider an input search field similar to Google's search bar. If a user starts typing in the field, we don't want to immediately send a request for each character they type. Instead, we can use debounce time to delay the request and ensure it is sent only after the user has paused typing for a certain duration. By applying debounce time, we can efficiently manage user input and prevent unnecessary API calls, which can lead to a better user experience and improved application performance. Now let's see how to use the debounce time for our Angular application.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use Debounce Time in Angular
&lt;/h2&gt;

&lt;p&gt;Most Common approach is to apply debounce time by using the &lt;strong&gt;rxjs&lt;/strong&gt; library. Which Angular already includes as a dependency.&lt;/p&gt;

&lt;p&gt;And now Let's explore those operators and pipes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;debounceTime() pipe&lt;/strong&gt;&lt;br&gt;
The debounceTime pipe is a built-in Angular pipe that can be used to debounce an event. To use the debounceTime pipe, you need to pass the debounce time in milliseconds as an argument&lt;br&gt;
&lt;strong&gt;distinctUntilChanged()&lt;/strong&gt;&lt;br&gt;
distinctUntilChanged is an operator that is used to filter out consecutive emissions of the same value from an Observable. This can be useful in situations where you want to prevent the Observable from emitting the same value multiple times in a row.&lt;br&gt;
&lt;strong&gt;takeUntil()&lt;/strong&gt;&lt;br&gt;
The takeUntil() operator in Angular is used to automatically unsubscribe from an Observable when another Observable emits a value. This can be useful in situations where you want to prevent an Observable from emitting values after a certain point.&lt;/p&gt;

&lt;p&gt;In this tutorial, I will show you how to use RxJS to apply debounce time to an input event. I will demonstrate way to implement debounce time in your application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 01: Create the template file
&lt;/h2&gt;

&lt;p&gt;In your template content&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 class="content" role="main"&amp;gt;
  &amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;
  &amp;lt;h2&amp;gt;How to Use Debounce Time to Improve the Performance of Your Angular Applications&amp;lt;/h2&amp;gt;
  &amp;lt;input type="text" 
  [(ngModel)]="inputText" 
  placeholder="Input something" (input)="onSearch()" /&amp;gt;
&amp;lt;br/&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, I have implemented an input field with an input event that triggers the debounce function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 02 : Import the required modules
&lt;/h2&gt;

&lt;p&gt;Open the component file and import the necessary modules&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, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { debounceTime, takeUntil } from 'rxjs/operators';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I import the required modules: &lt;strong&gt;Subject&lt;/strong&gt; and the &lt;strong&gt;debounceTime&lt;/strong&gt; operator from &lt;em&gt;rxjs/operators&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 03 : Implement the debounce logic
&lt;/h2&gt;

&lt;p&gt;Create a private &lt;strong&gt;searchSubject&lt;/strong&gt;,which will emit values whenever the &lt;strong&gt;onSearch&lt;/strong&gt; method is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class AppComponent {
  title = 'CodeSandbox';
  private searchSubject = new Subject&amp;lt;string&amp;gt;();
  inputText: string = '';
  ngOnInit() {

  }
  onSearch() {
    this.searchSubject.next(this.inputText);
  }

}

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

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;ngOnInit&lt;/em&gt; lifecycle hook, I have established a subscription to the &lt;em&gt;searchSubject&lt;/em&gt;. Within this subscription, I utilize the &lt;em&gt;debounceTime&lt;/em&gt; operator to introduce a delay before processing the emitted events. This delay allows the timer to reset if the user continues typing within the specified time (in this case, 300ms). However, if the user pauses typing for the specified time, the &lt;em&gt;searchSubject&lt;/em&gt; emits the latest value, triggering the execution of the &lt;em&gt;performSearch&lt;/em&gt; method with that value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class DebounceExampleComponent implements OnInit, OnDestroy {
  private searchSubject = new Subject&amp;lt;string&amp;gt;();
  private readonly debounceTimeMs = 300; // Set the debounce time (in milliseconds)

  ngOnInit() {
    this.searchSubject.pipe(debounceTime(this.debounceTimeMs)).subscribe((searchValue) =&amp;gt; {
      this.performSearch(searchValue);
    });
  }

  ngOnDestroy() {
    this.searchSubject.complete();
  }

  onSearch(searchValue: string) {
    this.searchSubject.next(searchValue);
  }

  performSearch(searchValue: string) {
    // Perform the actual search operation here
    console.log('Performing search for:', searchValue);
    // ... Your search logic ...
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Debounce time is a powerful technique to control the frequency of event execution in Angular applications. By strategically delaying the processing of input events, you can optimize the performance and responsiveness of your application. Whether it's handling user inputs or other frequent events, debounce time is a valuable tool to have in your Angular development toolbox.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That’s It!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course you probably want to see this in action. Here’s a working demo for you to fork or play with:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/modest-chaplygin-p87rrq"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Thanks for reading the whole story ❤&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Implementing Lazy Loading from Angular ⭐</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Tue, 25 Jul 2023 19:25:43 +0000</pubDate>
      <link>https://dev.to/mana95/how-to-implement-lazy-loading-in-your-angular-application-4gi1</link>
      <guid>https://dev.to/mana95/how-to-implement-lazy-loading-in-your-angular-application-4gi1</guid>
      <description>&lt;p&gt;In this article, I will delve into the implementation process of Lazy Loading in your Angular application. Additionally, I will provide a step-by-step guide on how to do lazy loading for you application.&lt;/p&gt;

&lt;p&gt;Before delving into the concept of lazy loading, it is essential to have a clear understanding of routing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Angular Routing
&lt;/h2&gt;

&lt;p&gt;Angular routing is a mechanism that allows you to navigate between different views and components in your Angular application. It helps you build single-page applications (&lt;a href="https://www.bloomreach.com/en/blog/2018/what-is-a-single-page-application" rel="noopener noreferrer"&gt;SPAs&lt;/a&gt;)by dynamically changing the content displayed on the page without reloading the entire page. Instead of traditional server-side routing, where the server handles navigation and page loads, Angular routing handles navigation on the client-side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why we want Lazy loading to our Application
&lt;/h2&gt;

&lt;p&gt;Lazy loading is essential for optimizing the performance and user experience of your application. Lazy loading allows your application to load only the essential components and modules needed for the initial view. Other part of component are loading if user navigate to through the application. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Angular Lazy Loading
&lt;/h2&gt;

&lt;p&gt;Angular provides a powerful feature called "&lt;em&gt;&lt;strong&gt;Lazy Loading&lt;/strong&gt;&lt;/em&gt;" which allows developers to load modules and components on-demand. Lazy loading is technique that allow user to defer loading of certain parts of an angular application. Parts mean certain modules in the application. These modules are loaded dynamically when a user navigate to a route associated with that module, It will reduce the initial load time as well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Lazy loading to our Angular Application
&lt;/h2&gt;

&lt;p&gt;In your Angular Application you need to create feature modules that encapsulate specific parts of your application. These modules can be loaded independently, and you can specify which modules should be lazy-loaded based on the routes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Configure Routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;in side your &lt;strong&gt;app.component.html&lt;/strong&gt; template you need to defined the &lt;br&gt;
&lt;code&gt;&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt;&lt;/code&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;{{title}}&amp;lt;/h2&amp;gt;

&amp;lt;button class="btn btn-primary btn-sm" style="margin: 1%;"  routerLink="/users"&amp;gt;Users&amp;lt;/button&amp;gt;
&amp;lt;button class="btn btn-primary btn-sm" style="margin: 1%;" routerLink="/home"&amp;gt;Home&amp;lt;/button&amp;gt;

&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt;




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

&lt;/div&gt;

&lt;p&gt;In your main &lt;strong&gt;app-routing.module.ts&lt;/strong&gt;, configure your routes using the &lt;strong&gt;loadChildren&lt;/strong&gt; property instead of component. The loadChildren property takes a string that points to the relative path of the lazy-loaded module. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; LoadChildren is use for lazy loading&lt;/p&gt;
&lt;/blockquote&gt;

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

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path:'',
    redirectTo:'',
    pathMatch:'full'
  },
  {
    path:'users',
    loadChildren:()=&amp;gt; import('./users/users.module').then(m=&amp;gt; m.UsersModule)
  },
  {
    path:'home',
    component:HomeComponent
  }

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }



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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create the Users Modules&lt;/strong&gt;&lt;br&gt;
To use Lazy Loading, we need to create feature modules that encapsulate specific parts of our application. Each feature module contains its components, services, and other related files.&lt;/p&gt;

&lt;p&gt;Open the terminal command and create new component called users component by running the command &lt;/p&gt;

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

ng generate component users


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

&lt;/div&gt;

&lt;p&gt;Create the users Module(in this case, users.module.ts),&lt;/p&gt;

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

ng generate module users


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: You can create the above module and component manually instead of executing commands&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Apart from that, we need to create a feature routing module under the &lt;strong&gt;users-routing.module.ts&lt;/strong&gt; file. It should be defined in the &lt;strong&gt;users.module.ts&lt;/strong&gt; file under the imports section.&lt;/p&gt;

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

  imports: [
    CommonModule,
    UsersRoutingModule
  ]


&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;

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UsersComponent } from './users.component';

const routes: Routes = [
 {
    path:'',
    component:UsersComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UsersRoutingModule { }



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

&lt;/div&gt;

&lt;p&gt;Here is the folder structure after generating the component and module file&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%2Flr8swth98eq0o1i0rslg.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%2Flr8swth98eq0o1i0rslg.png" alt="Image description" width="341" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Defined Child routing from the users module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need to created two separate component called &lt;strong&gt;users-list&lt;/strong&gt; and &lt;strong&gt;users-detail&lt;/strong&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%2Fhz2xgyqkrk5xdvva6heh.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%2Fhz2xgyqkrk5xdvva6heh.png" alt="Image description" width="395" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;inside the &lt;strong&gt;users.module.ts&lt;/strong&gt; we need to defined the two component under  the  &lt;strong&gt;declarations&lt;/strong&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';
import { UsersComponent } from './users.component';
import { UsersRoutingModule } from './users-routing.module';
import { UsersListComponent } from './users-list/users-list.component';
import { UserDetailComponent } from './users-list/user-detail/user-detail.component';



@NgModule({
  declarations: [
    UsersComponent,
    UsersListComponent,
    UserDetailComponent
  ],
  imports: [
    CommonModule,
    UsersRoutingModule
  ]
})
export class UsersModule { }



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

&lt;/div&gt;

&lt;p&gt;After that we need to defined the feature child routing under the &lt;em&gt;users-routing.module.ts&lt;/em&gt; under the &lt;strong&gt;routes&lt;/strong&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 { RouterModule, Routes } from '@angular/router';
import { UsersComponent } from './users.component';
import { UsersListComponent } from './users-list/users-list.component';
import { UserDetailComponent } from './users-list/user-detail/user-detail.component';

const routes: Routes = [
  {
    path:'',
    component:UsersComponent
  },
  {
    path:'user-list',
    component:UsersListComponent
  },
  {
    path:'user/:id',
    component:UserDetailComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UsersRoutingModule { }


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Defined Child routing handling Templates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;em&gt;users.component.html&lt;/em&gt; file, we have set up navigation for the child routing using the &lt;strong&gt;routerLink&lt;/strong&gt; directive. This allows us to navigate to different child routes when the user clicks on specific links. Here's an example of how it's done:&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;User Component&amp;lt;/h2&amp;gt;

&amp;lt;button class="btn btn-warning btn-sm" style="margin: 1%;" [routerLink]="['/users','user-list']"&amp;gt;User List&amp;lt;/button&amp;gt;
&amp;lt;button class="btn btn-warning btn-sm" style="margin: 1%;" [routerLink]="['/users','user','123']"&amp;gt;User Details&amp;lt;/button&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;For Explanation about above **routerLink&lt;/strong&gt;**&lt;br&gt;
The [routerLink] attribute can be bound to an expression, and in the &lt;code&gt;[routerLink]="['/users','user-list']&lt;/code&gt; the expression is ['/users', 'user-list']. This expression is an array of path segments, where each element represents a part of the route URL.&lt;/p&gt;

&lt;p&gt;Let's break down the expression:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;'/users'&lt;/strong&gt; : Representing the parent route. It indicates that the navigation will happen relative to the 'users' route&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;'user-list'&lt;/strong&gt; : Representing the child route. It indicates that we want to navigate to the 'user-list' route, which is a child route of the 'users' route&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
Angular Lazy Loading is a powerful feature that optimizes performance by loading modules and components on-demand. By strategically employing Lazy Loading, we can reduce the initial load time and improve the overall user experience&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That’s It!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course you probably want to see this in action. Here’s a working demo for you to fork or play with:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/p/github/Mana95/dev-to-angular/lazy-loading?layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clkio7trv017v356mdtukdp0k%2522%252C%2522sizes%2522%253A%255B70%252C30%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clkio7trv017r356m4him7gcy%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clkio7trv017t356mxpwi6iab%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clkio7trv017u356m21szuypz%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B50%252C50%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clkio7trv017r356m4him7gcy%2522%253A%257B%2522id%2522%253A%2522clkio7trv017r356m4him7gcy%2522%252C%2522activeTabId%2522%253A%2522clkiod4ym00hc356msxt4zsnk%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522PR_INFO%2522%252C%2522id%2522%253A%2522clkiod4ym00hc356msxt4zsnk%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%257D%252C%2522clkio7trv017u356m21szuypz%2522%253A%257B%2522id%2522%253A%2522clkio7trv017u356m21szuypz%2522%252C%2522activeTabId%2522%253A%2522clkio910i01of356mb3lukqd4%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522TASK_PORT%2522%252C%2522taskId%2522%253A%2522start%2522%252C%2522port%2522%253A4200%252C%2522id%2522%253A%2522clkio910i01of356mb3lukqd4%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%2522%257D%255D%257D%252C%2522clkio7trv017t356mxpwi6iab%2522%253A%257B%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clkio7trv017s356m2ia0xfpu%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522start%2522%257D%255D%252C%2522id%2522%253A%2522clkio7trv017t356mxpwi6iab%2522%252C%2522activeTabId%2522%253A%2522clkio7trv017s356m2ia0xfpu%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Atrue%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub : &lt;a href="https://github.com/Mana95/dev-to-angular" rel="noopener noreferrer"&gt;dev-to-angula lazy loading&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading the whole story ❤&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>angular</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How to use mergeMap and forkJoin to handle multiple API requests in Angular 🚀</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Mon, 17 Jul 2023 17:00:47 +0000</pubDate>
      <link>https://dev.to/mana95/how-to-use-mergemap-and-forkjoin-to-handle-multiple-api-requests-in-angular-412p</link>
      <guid>https://dev.to/mana95/how-to-use-mergemap-and-forkjoin-to-handle-multiple-api-requests-in-angular-412p</guid>
      <description>&lt;p&gt;In this Article, I will explore the process of how to handle multiple API requests in Angular. Handling multiple API requests in Angular is a common scenario in modern web development. It's essential to implement an efficient and maintainable solution to avoid issues such as &lt;strong&gt;nested subscriptions&lt;/strong&gt;. In this article, we will explore the proper way to handle multiple API requests using the &lt;em&gt;mergeMap&lt;/em&gt; and &lt;em&gt;forkJoin&lt;/em&gt; operators from the Angular RxJS library.&lt;/p&gt;

&lt;p&gt;First of all, I will explain the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  problem because we are not using nested subscriptions
&lt;/h2&gt;

&lt;p&gt;In the real world, it is common to make multiple API calls in our web applications. When navigating to a page, it is often necessary to retrieve data from various APIs. In Angular, the subscribe operator is typically used to fetch data from a single API call. However, when dealing with multiple API calls, there are more effective methods to implement this. We can initially solve the problem using the &lt;strong&gt;subscribe&lt;/strong&gt; operator and then improve upon it by utilizing &lt;strong&gt;mergeMap&lt;/strong&gt; and &lt;strong&gt;forkJoin&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Subscribe()
&lt;/h2&gt;

&lt;p&gt;Normally, this technique is quite simple. However, when we trigger a new subscription for each and every value emitted by the Observable of &lt;strong&gt;call()&lt;/strong&gt;, it can result in the creation of numerous unhandled Observables and Subscriptions. All of these entities remain in memory for the time being, leading to potential issues. Here is nested subscription example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class DevToComponent implements OnInit {
    todoValue: string = '';
    comment: any;

    constructor(private http: HttpClient) { }

    ngOnInit(): void {

        this.retrieveDefaultData();
    }

    retrieveDefaultData(): void {
        this.http.get('https://jsonplaceholder.typicode.com/posts/1')
            .pipe(map(todo =&amp;gt; todo[0]))
            .subscribe(
                todo =&amp;gt; {
                    this.todoValue = todo;
                    this.http.get(`https://jsonplaceholder.typicode.com/posts/${this.todoValue}/comments`)
                        .subscribe(
                            coment =&amp;gt; {
                                this.comment = coment;
                            }
                        );
                }
            )
    }

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

&lt;/div&gt;



&lt;p&gt;It's important to note that when you &lt;strong&gt;subscribe()&lt;/strong&gt; to an Observable multiple times, your subscribers won't listen to the exact same source. Instead, each subscription triggers a new copy of the defined &lt;a href="https://rxjs-dev.firebaseapp.com/guide/observable"&gt;Observable&lt;/a&gt; pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  MergeMap()
&lt;/h2&gt;

&lt;p&gt;This operator takes an input observable, applies a transformation function to each emitted value, and merges the resulting observables into a single output observable.By using &lt;a href="https://www.techgeeknext.com/angular/angular-rxjs-mergemap-operator"&gt;MergeMap()&lt;/a&gt;, we can avoid nested subscriptions and achieve a more streamlined and efficient approach to handling asynchronous requests.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can use the MergeMap() when we need data information from first API request to second API request:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class DevToComponent implements OnInit {
    todoValue: string = '';
    comment: any;
    mergeMapResult: any;
    constructor(private http: HttpClient) { }

    ngOnInit(): void {
        this.retrieveDefaultData();
    }

    retrieveDefaultData(): void {
        this.http.get('https://jsonplaceholder.typicode.com/posts/1')
            .pipe(
                map(todo =&amp;gt; {
                    return todo
                }),
                mergeMap(todo =&amp;gt; this.http.get(`https://jsonplaceholder.typicode.com/posts/${todo}/comments`))
            ).subscribe(response =&amp;gt; {
                this.mergeMapResult = response;
            })
    }

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

&lt;/div&gt;



&lt;p&gt;Using mergeMap avoids nested subscriptions, you can avoid nested subscriptions and flatten the asynchronous operations. This leads to cleaner and more readable code, as the operations are executed sequentially instead of nesting multiple subscription callbacks.&lt;/p&gt;

&lt;h2&gt;
  
  
  ForkJoin()
&lt;/h2&gt;

&lt;p&gt;This operator,when you have multiple parallel API requests that are independent of each other and you want to wait for all of them to complete before taking further action, you can use the &lt;a href="https://rxjs.dev/api/index/function/forkJoin"&gt;forkJoin()&lt;/a&gt; operator instead of nested subscriptions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can use the ForkJoin() calling independent multiple API requests:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class DevToComponent implements OnInit {
    todoValue: string = '';
    comment: any;
    firstApiResult: any;
    secondApiResult: any;
    constructor(private http: HttpClient) { }

    ngOnInit(): void {
        this.retrieveDefaultData();
    }

    retrieveDefaultData(): void {
        const firstAPI = this.http.get('https://jsonplaceholder.typicode.com/posts/1')
        const secondAPI = this.http.get(`https://jsonplaceholder.typicode.com/posts`)

        forkJoin([firstAPI, secondAPI]) //we can use more that 2 api request 
            .subscribe(
                result =&amp;gt; {
                    //this will return list of array of the result
                    this.firstApiResult = result[0];
                    this.secondApiResult = result[1];
                }
            )
    }

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

&lt;/div&gt;



&lt;p&gt;Using forkJoin() simplifies the handling of parallel API requests by providing a clean and efficient way to wait for all requests to complete. It eliminates the need for nested subscriptions, improving code readability and maintainability.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : Note that if any of the source observables in forkJoin() errors before completing, the entire observable will error and no result will be emitted. So, it's important to handle error cases appropriately when using forkJoin().&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
By adopting the use of mergeMap() and forkJoin() in RxJS, you have acquired a powerful technique for handling multiple API requests without resorting to nested subscriptions. This approach not only improves the readability and maintainability of your code but also allows for efficient parallel execution of asynchronous operations.&lt;/p&gt;

&lt;p&gt;Thanks for reading the whole story ❤&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>angular</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How to Handle Dynamic Form fields in Reactive forms in Angular</title>
      <dc:creator>Manoj Prasanna  🚀</dc:creator>
      <pubDate>Sun, 16 Jul 2023 11:06:58 +0000</pubDate>
      <link>https://dev.to/mana95/how-to-handle-dynamic-form-fields-in-reactive-forms-in-angular-3c0f</link>
      <guid>https://dev.to/mana95/how-to-handle-dynamic-form-fields-in-reactive-forms-in-angular-3c0f</guid>
      <description>&lt;p&gt;In this article, I will explore the process of handling dynamic forms in the Angular framework using Reactive Forms. Dynamic forms allow us to dynamically generate form fields based on user input or other conditions. By leveraging the power of Reactive Forms, we can easily handle dynamic form interactions in Angular. Let's dive into the step-by-step process of creating and managing dynamic forms in Angular.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Reactive Forms&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;First we will explain what is reactive form in angular,&lt;br&gt;
Reactive forms are build around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.In Angular their have two type of forms ,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Template-driven forms&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reactive Forms&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Related to above form type the suitable to create dynamic form from Reactive forms, Lets started with building simple sample application.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Scenario&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step-01 : Setting up the Angular project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, Let's create a new Angular project using the Angular CLI.&lt;br&gt;
Open your command prompt(terminal) and run the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng new dynamic-forms-demo


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step-02 : Create the new component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new component for the dynamic form&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng generate component dynamic-form


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

&lt;/div&gt;
&lt;p&gt;another way&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng g c dynamic-form


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

&lt;/div&gt;
&lt;p&gt;Now you will create a component name call &lt;strong&gt;dynamic-form&lt;/strong&gt; under the "src" folder&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%2Fpfy1vcn25834yv2bc7ij.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%2Fpfy1vcn25834yv2bc7ij.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Import the Required Modules&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to work with Reactive Forms, we need to import the &lt;strong&gt;ReactiveFormsModule&lt;/strong&gt; from &lt;strong&gt;@angular/forms&lt;/strong&gt; module. Open the &lt;strong&gt;app.module.ts&lt;/strong&gt; file in your project and add the following import statement:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { ReactiveFormsModule } from '@angular/forms';


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

&lt;/div&gt;
&lt;p&gt;Then include the &lt;strong&gt;ReactiveFormsModule&lt;/strong&gt; to the imports sections&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@NgModule({
  declarations: [
    // Other declarations
  ],
  imports: [
    // Other imports
    ReactiveFormsModule
  ],
  providers: [
    // Other providers
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4: Set Up the Reactive Form&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;doing inside of the dynamic-form.component.ts&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Import the &lt;strong&gt;FormArray, FormBuilder, FormGroup&lt;/strong&gt; to the dynamic-form component&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { FormArray, FormBuilder, FormGroup } from '@angular/forms';


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

&lt;/div&gt;
&lt;p&gt;Then,inside the class define the &lt;strong&gt;dynamicFormGroup&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export class DynamicFormComponent implements OnInit {

  dynamicFormGroup!: FormGroup;  //defined the dynamicFormGroup

  constructor() { }

  ngOnInit() {

  }

}


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

&lt;/div&gt;
&lt;p&gt;In the component's constructor, initialize the form group and form array&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export class DynamicFormComponent implements OnInit {

  dynamicFormGroup!: FormGroup;

  constructor(private formBuilder : FormBuilder) { }

  ngOnInit() {
    this.dynamicFormGroup = this.formBuilder.group({
          Address: new FormArray([])
    });
  }
}


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

&lt;/div&gt;
&lt;p&gt;Then create the dynamic fields for the &lt;strong&gt;Address&lt;/strong&gt; &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

get createItem(): FormGroup {
    return this.formBuilder.group({
      streetAddress: [],
      city:[],
      state:[]
    })
  }


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

&lt;/div&gt;
&lt;p&gt;Now we need to add the &lt;strong&gt;createItem&lt;/strong&gt; method fields for the Address group, The &lt;strong&gt;createItem&lt;/strong&gt; method creates a new instance of the &lt;strong&gt;FormGroup&lt;/strong&gt; for the Address group, with the corresponding form control fields (streetAddress, city, state,) and their initial values set to an empty string.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 ngOnInit() {
    this.dynamicFormGroup = this.formBuilder.group({
      Address: new FormArray([this.createItem]),
    });
  }


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5: Build the Dynamic Form Template&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's construct the template for our dynamic form in the &lt;em&gt;dynamic-form.component.html&lt;/em&gt; file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 &amp;lt;div&amp;gt;
    &amp;lt;form [formGroup]="dynamicFormGroup" (ngSubmit)="onSubmit()"&amp;gt;
        &amp;lt;div class="form-row" *ngFor="let fields of AddressInfo.controls; let i = index" &amp;gt;
            &amp;lt;div class="form-group col-md-3" &amp;gt;
                &amp;lt;label for="password"&amp;gt;&amp;lt;b&amp;gt;Street Address&amp;lt;/b&amp;gt;&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" class="form-control" placeholder="Street Address" name="SA" formControlName="streetAddress" /&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group col-md-3" &amp;gt;
                &amp;lt;label for="password"&amp;gt;&amp;lt;b&amp;gt;City&amp;lt;/b&amp;gt;&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" class="form-control" placeholder="City" name="city" formControlName="city" /&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div class="form-group col-md-3" &amp;gt;
                &amp;lt;label for="password"&amp;gt;&amp;lt;b&amp;gt;State&amp;lt;/b&amp;gt;&amp;lt;/label&amp;gt;
                &amp;lt;input type="text" class="form-control" placeholder="State" name="state" formControlName="state" /&amp;gt;
              &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;br/&amp;gt;
     &amp;lt;button type="submit"  class="btn btn-primary"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
    &amp;lt;br/&amp;gt;
    &amp;lt;button type = "button" class="btn btn-primary" (click)="addNewAddress()" &amp;gt;Add New Address&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;    

&amp;lt;h4&amp;gt;OUTPUT&amp;lt;/h4&amp;gt;
&amp;lt;div&amp;gt;{{output}}&amp;lt;/div&amp;gt;


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 7: Display and Interact with the Dynamic Form&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the template, add a button to trigger the addition of form fields dynamically:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;button type = "button" class="btn btn-primary" (click)="addNewAddress()" &amp;gt;Add New Address&amp;lt;/button&amp;gt;


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

&lt;/div&gt;
&lt;p&gt;Additionally, you can bind the form array's controls to display the dynamic form fields&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;div class="form-row" *ngFor="let fields of AddressInfo.controls; let i = index" &amp;gt;
&amp;lt;!-- Display form fields dynamically --&amp;gt;
&amp;lt;/div&amp;gt;


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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 8: Handle Form Submission&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To handle the form submission, create a method in the component:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

onSubmit() {
//logic implementation
this.output = this.dynamicFormGroup.controls['address'].value;
    console.log(this.output);
}


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

&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;Conclusion:&lt;br&gt;
By following these step-by-step instructions, you have learned how to create dynamic forms in Angular using Reactive Forms. Leveraging the power of Reactive Forms and Angular's capabilities, you can now handle and manage dynamic forms seamlessly. This knowledge opens up new possibilities for building interactive and user-friendly applications in Angular. Happy coding!&lt;br&gt;
Here is the Example Solution :&lt;a href="https://codesandbox.io/p/sandbox/hardcore-goldberg-ytsxn5?file=%2Fsrc%2Fapp%2Fdynamic-form%2Fdynamic-form.component.ts%3A35%2C16" rel="noopener noreferrer"&gt;Angular Dynamic Forms handling reactive forms&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;That’s It!&lt;/strong&gt;&lt;br&gt;
Of course you probably want to see this in action. Here’s a working demo for you to fork or play with:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/hardcore-goldberg-ytsxn5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading the whole story ❤&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PC : &lt;a href="https://morioh.com/p/3cbcfff8086a" rel="noopener noreferrer"&gt;https://morioh.com/p/3cbcfff8086a&lt;/a&gt;&lt;/p&gt;

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