<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juan Camilo Giraldo Chaverra on Medium]]></title>
        <description><![CDATA[Stories by Juan Camilo Giraldo Chaverra on Medium]]></description>
        <link>https://medium.com/@camilogiraldo91?source=rss-fdc05816bc4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-4EYOTxaN-o2k5oxtuFk1w@2x.jpeg</url>
            <title>Stories by Juan Camilo Giraldo Chaverra on Medium</title>
            <link>https://medium.com/@camilogiraldo91?source=rss-fdc05816bc4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:04:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@camilogiraldo91/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular dynamic forms: ng-switch approach]]></title>
            <link>https://codeburst.io/angular-dynamic-forms-ng-switch-approach-4f267c01d2c6?source=rss-fdc05816bc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f267c01d2c6</guid>
            <category><![CDATA[dynami-forms]]></category>
            <category><![CDATA[angular-forms]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[ng-switch]]></category>
            <dc:creator><![CDATA[Juan Camilo Giraldo Chaverra]]></dc:creator>
            <pubDate>Wed, 03 Apr 2019 15:33:04 GMT</pubDate>
            <atom:updated>2024-07-23T20:11:22.626Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating angular dynamic forms using the ng-switch approach</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MJtMrw7eJuT_3XGFHre-KA.gif" /></figure><p>Nowadays forms are a crucial part of every application over the web, a signup/sign-in process, a payment process, a survey and an endless kinds of scenarios in our day to day life exploring the web.</p><p>Sometimes creating web applications goes way beyond the static and predefined forms, and there are some scenarios where having a dynamic way to create forms based on changing business requirements comes really handy in order to reduce the amount of time and resources spent supporting an application.</p><p>Some great examples of this are <em>survey monkey </em>or <em>google forms</em>, where they create their forms dynamically only using a configuration created by the user.</p><h3>Angular approaches</h3><h4>Template driven vs Model Driven</h4><p>Angular offers two approaches for handling input data using forms, you can opt for using <strong><em>Template Driven Forms</em></strong> or <strong><em>Model Driven Forms.</em></strong></p><p>In simple words</p><ol><li><em>Template Driven forms:</em> They are less explicit, all the form is configured over the template. If you have some basic requirements this is the way to go. <strong><em>Pros</em></strong>:It’s the simplest way to add forms into an angular applications, comes really handy for creating simple forms like such an email signup form. <strong><em>Cons: </em></strong>Don’t scale very well, and can become a little messy as an application grows.</li><li><em>Model Driven Forms:</em> They are used for more robust applications, providing a most scalable, reusable and testable approach as most of the logic is in the component. <strong><em>Pros: </em></strong>Already mentioned, more scalable, reusable and testable with a low-level API access. <strong><em>Cons: </em></strong>More verbose.</li></ol><p>So as you may guess, the approach we are taking for our use case will be the <em>Model Driven Approach </em>also known as <em>Reactive Approach. </em>For more information about angular forms, make sure to take a look to the official <a href="https://angular.io/guide/forms-overview">angular forms docs</a></p><h4>Getting Started</h4><p>Create a new project using <em>ng new dynamic-forms</em>and then navigate to the <em>dynamic-forms</em><strong><em> </em></strong>folder.<br>If you are using the latest version of the <em>angular-cli</em> it will ask you if you want to add routing, for this project I selected <em>no</em> and what kind of stylesheet format do you want to use for which I selected <em>sass</em>. So that will create a basic angular project for us to get started. <em>ng serve</em> should get us up and running. More info about the @angular/cli can be found <a href="https://cli.angular.io/">here</a>.</p><h3>Creating our dynamic-form component</h3><p>This component will be our wrapper for dynamically creating our form. So to do that simply generate a new component using angular-cli command <em>ng generate component</em></p><p><em>ng generate component components/dynamic-form</em></p><p>That will create a new component for us and now we can make use of it, for now let’s replace our <em>app.component.html</em> content with our new created component, that’s <em>&lt;app-dynamic-form&gt; &lt;/app-dynamic-form&gt;</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cbbaf2fb46ad4df496f1f0fa2bd06cd/href">https://medium.com/media/3cbbaf2fb46ad4df496f1f0fa2bd06cd/href</a></iframe><p>Now we have our basic project structure, it’s time to move ahead with our logic for creating a dynamic form.</p><p>First of all let’s define our form data model using an interface. Let’s say for instance that our data model will be the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de6f46d9e18abb1315810455344ca7a6/href">https://medium.com/media/de6f46d9e18abb1315810455344ca7a6/href</a></iframe><p>This will help us to type our mock data in order to keep it consistent with our defined model. In our real world use case this will type the server seed data which will dynamically create our form.</p><p>For our use case we will be using Angular Reactive Form approach, so in order to do that we need to add the following imports into our<em> </em><em>app.module.ts.</em></p><blockquote><em>Full documentation for angular reactive forms can be found </em><a href="https://angular.io/guide/reactive-forms#getting-started"><em>here</em></a></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a98ff3e605bcc371d455283c7d4b2cb/href">https://medium.com/media/6a98ff3e605bcc371d455283c7d4b2cb/href</a></iframe><p>This will help us with all the API required for using reactive forms, in our case we will be using <em>FormGroup</em> and <em>FormControl</em> to keep things simple.</p><p>From the official angular docs.</p><ul><li><strong>FormControl</strong>:<em> “Tracks the value and validation status of an individual form control.”</em></li><li><strong>FormGroup</strong>:<em> “Tracks the same values and status for a collection of form controls.”</em></li></ul><p>Let’s get to our dynamic-form component, so the first thing we need to do is to create our basic form. As the previous definition says, in order to track values and status for a collection of form controls AKA inputs we need to create a new instance of a <em>FormGroup</em> in our component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ad8615197630375071f7a6c82ef6917/href">https://medium.com/media/6ad8615197630375071f7a6c82ef6917/href</a></iframe><p>This will be for now our <em>DynamicForm</em> component. We’ve just imported <em>FormGroup</em> in order to create a <em>FormGroup</em> instance for our dynamic form. This way we can tie up our template form with the logic of our component, and the way to tie these two together is by using the <em>[FormGroup]</em> attribute binding in our template, passing our <em>form: FormGroup</em>created in our component as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6246e9d864fad2ad0b90743dafee709/href">https://medium.com/media/f6246e9d864fad2ad0b90743dafee709/href</a></iframe><p>By doing this now we have a direct connection between our form in the template and the logic behind it in our component.</p><h4><em>Mock Data</em></h4><p>Everything so good so far, things are becoming more interesting. For our use case we will be using some mock data in order to recreate some kind of data fetching from a remote location — lets say an API —</p><p>So we will have some mock data based on the FormData interface defined previously. This basically will provide us the basic information for every of our form fields like the name, placeholder, type, validators, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2561efd780ce14d47da501fe35f5700/href">https://medium.com/media/b2561efd780ce14d47da501fe35f5700/href</a></iframe><p>To make things simple we will import our mock data into our <em>app.component.ts</em>and then pass it down to our dynamic-form component as an <em>@Input().</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dc56ffbcb7a6f57f856db263f4d9c1d/href">https://medium.com/media/8dc56ffbcb7a6f57f856db263f4d9c1d/href</a></iframe><p>So we created an instance of a MockForm object. Following next we will be accepting an <em>@Input() formData</em>of type <em>FormData[]. </em>Then, we are accepting the <em>data </em>mockForm instance in our <em>dynamic-form template</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a55f6e589819ba71c96bddaabc5d0373/href">https://medium.com/media/a55f6e589819ba71c96bddaabc5d0373/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e00906e3c775654cd27808b10b7dccb5/href">https://medium.com/media/e00906e3c775654cd27808b10b7dccb5/href</a></iframe><blockquote>In a real world scenario, this data should be retrieved from an external API or something similar.</blockquote><p>Now we have our data available in our component, the next step is to create the <em>FormGroup</em> with each of the <em>FormControl</em>’s or form fields that our dynamic form will have. That will take place in our <em>ngOnInit</em> lifecycle hook.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8593a6f8ee51ea8ff76bff8b3ade300/href">https://medium.com/media/f8593a6f8ee51ea8ff76bff8b3ade300/href</a></iframe><p>We have just simply looped through the formData, and created a new <em>FormControl</em> for every form field in the array. Then we instantiate a<em> new FormGroup</em> using those FormControls to keep track of them.</p><p>The only thing missing now is to track every form field in our form template by using the already created FormGroup instance.</p><h4>Using ngSwitch</h4><p>Now we need a way for dynamically create our template, the most simple approach for this is using the ngSwitch directive. First of all we need to loop over the <em>formData</em> in order to get access to every <em>formData</em> element. And next just switch over every <em>controlType</em> to get to know what kind of input we need to display on the UI.</p><blockquote>ControlType was just a generic name, you can use whatever name you want to define for the properties of your dynamic form.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87e70e45970c9b4ea719664381cc16f0/href">https://medium.com/media/87e70e45970c9b4ea719664381cc16f0/href</a></iframe><p>As we are already looping through the data, the next step to follow is to ask for what kind of input do we want to display on the UI.</p><p>By using <em>ngSwitchCase</em> we can validate what kind of control type we have as an element in the loop run. In the first case let’s handle the scenario where we have an input of type ‘text’.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/005c837a109cd6452981da9355b1d102/href">https://medium.com/media/005c837a109cd6452981da9355b1d102/href</a></iframe><p>First of all we need to bind the <em>controlName </em>into the <em>formControName </em>in order to point to our previously created FormControl instance. Note that we are using the <em>controlName</em> property of our <em>data </em>as we previously did in the creation of the <em>FormGroup</em> instance in our component. This is a MUST in order to tie up the template inputs with the form fields instances.</p><p>Then we just bound type and name attributes with the valueType and controlName data properties respectively. Finally for the sake of simplicity we bound the basic validations into our input template. This also could have been done in the component when we were creating our <em>FormGroup</em> instance. It’s just matter of personal preference.</p><p>And last but not least, we are handling error use cases based on the validity of the form. You can learn more about form error and validations <a href="https://angular.io/guide/form-validation">here</a>.</p><p>We can do the same for allowing another input types like ‘radio’ and ‘selects’ in our case this will be the template for creating both of them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b948d9f4bb6badec7d87780d353beb0d/href">https://medium.com/media/b948d9f4bb6badec7d87780d353beb0d/href</a></iframe><p>And with this, we have all in order to dynamically render our form based on some mock data. The following codesandbox is a running example of our use case.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F2o65vyoqyr%3Ffontsize%3D14&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F2o65vyoqyr%3Ffontsize%3D14&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F2o65vyoqyr%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/23452108b553b835d82edff8a17d65d9/href">https://medium.com/media/23452108b553b835d82edff8a17d65d9/href</a></iframe><p>This way you can power up your applications for creating dynamic content , in this case a forms use case with a really straightforward approach.</p><p>We’ve just scratched the surface, and using this simple starter guide will help you to cover more complex scenarios like, custom validations, adding custom inputs, predefined values, etc. It just takes a really good understanding of how form approach works on angular and start playing with all the alternatives that the @angular/forms API provides. You can refer to the <a href="https://angular.io/guide/reactive-forms"><em>Reactive Forms</em></a><em>, </em><a href="https://angular.io/guide/forms#template-driven-forms"><em>Template Forms</em></a><em>, </em><a href="https://angular.io/guide/form-validation"><em>Forms Validation</em></a><em> &amp; </em><a href="https://angular.io/guide/dynamic-form"><em>Dynamic Forms</em></a><em> </em>official documentation.</p><blockquote>You can check the full source code on <a href="https://github.com/camilogiraldo/dynamic-forms"><em>Github</em></a></blockquote><blockquote>I’m looking for a new gig. Strong experience working on highly dynamic applications using both angular and recatjs. Dms open on X formerly twitter @camilogiraldo</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f267c01d2c6" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/angular-dynamic-forms-ng-switch-approach-4f267c01d2c6">Angular dynamic forms: ng-switch approach</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lazy loading Angular Modules]]></title>
            <link>https://medium.com/angularmedellin/lazy-loading-angular-modules-58fc844b5c7d?source=rss-fdc05816bc4------2</link>
            <guid isPermaLink="false">https://medium.com/p/58fc844b5c7d</guid>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular-router]]></category>
            <dc:creator><![CDATA[Juan Camilo Giraldo Chaverra]]></dc:creator>
            <pubDate>Wed, 24 Jan 2018 03:17:22 GMT</pubDate>
            <atom:updated>2024-07-23T20:10:46.603Z</atom:updated>
            <content:encoded><![CDATA[<p>Angular provides an efficient, elegant and easy way to manage module loading at runtime as soon as the user requests certain features of our application. Other than being ‘<em>feature fancy</em>’, its a way to reduce our application bundle via outsourcing application features that are not required at startup time.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FMP1kygLQzjCve%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FMP1kygLQzjCve%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FMP1kygLQzjCve%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/a944c394179ba118c05e3882296f1791/href">https://medium.com/media/a944c394179ba118c05e3882296f1791/href</a></iframe><p>Thanks to <a href="https://angular.io/api/router">@angular/router</a> package, implementing this in our applications is easier than you might think. So let’s get started.</p><h3>1. Creating our million dollar fancy application</h3><p>We’re going to use Angular CLI to create our application, let’s create a new app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/847e690e02c9879d6f577c1659d0788c/href">https://medium.com/media/847e690e02c9879d6f577c1659d0788c/href</a></iframe><blockquote>For those who are getting started, this command will create an angular application and its basic structure, followed by the dependency installation. For more please refer to<a href="https://github.com/angular/angular-cli"> @angular/cli</a>.</blockquote><p>Notice I passed the <em>routing </em>flag to our <em>ng new </em>command, this will generate the routing for our application.</p><h3>2. Creating our main components</h3><p>Let’s suppose we’re going to have two main components in our app, the former is going to be the HomeComponent where our users will visit our application when they hit let’s say <em>fancy-app.fancydomain.</em></p><p>The latter, our BigFeatureComponent that’s gonna be the one we will <em>Lazy Load</em> as the user navigates through our application. So let’s create them using angular CLI.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f85d72db406c82b2703c6d2776cb43ba/href">https://medium.com/media/f85d72db406c82b2703c6d2776cb43ba/href</a></iframe><blockquote>For the purpose of this tutorial, we are skipping the creation of test files passing the spec=false flag. Do not forget to add tests to your production ready applications.</blockquote><p>This way angular CLI will <em>automagically</em> create our two new components and updates our <em>AppModule</em> for us with its respective declarations. More info about @angular/cli commands can be found <a href="https://github.com/angular/angular-cli">here</a></p><p>For now our project structure should look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*tAbZoyvD6bPpotd7b7rLfw.png" /></figure><h3>3. Adding features to our Application</h3><p>For now, we have all our application structure generated. Now is time to start adding those <em>game-changing</em> features.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FtoXKzaJP3WIgM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FtoXKzaJP3WIgM%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExeTZyazE4aGtqeXdxZTVsbjQ1cXg3bmNhMXd0bWx1ZmtkcGN4eXhxbCZlcD12MV9naWZzX2dpZklkJmN0PWc%2FtoXKzaJP3WIgM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="261" frameborder="0" scrolling="no"><a href="https://medium.com/media/655d7c648c376b31edea59577be27bf1/href">https://medium.com/media/655d7c648c376b31edea59577be27bf1/href</a></iframe><p>First we’re going to create the routing for our application, which will be in charge of mocking up the navigation of our application as the users perform application tasks. Open up our<em> app-routing.module.ts </em>and complete it up with the following routes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a67ea23dee6d17c908ac0a809cd6caa9/href">https://medium.com/media/a67ea23dee6d17c908ac0a809cd6caa9/href</a></iframe><blockquote>For a more detailed information about how the @angular/router package works, here’s the <a href="https://angular.io/guide/router">official documentation.</a></blockquote><p>By adding this, our angular app will register the path routes /<em>home</em> and /<em>bigComponent </em>and make them available to the user, as they use our app show them the appropriate content, <em>HomeComponent </em>and <em>BigFeatureComponent </em>respectively.</p><p>Each of the components will have its own <em>game changer</em> feature, but for the purpose of this blog post I’ll keep things simple and will focus in what really matters, how to Lazy load an angular Module. More on that later.</p><p>I added each of the respectively anchor tags to our <em>HomeComponent </em>and <em>BigFeatureComponent </em>HTML’s, to be able to navigate through our application components.</p><p>Our <em>home-component.component.html</em> should look like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d78b02e1cf6dbcf08d85cdd82f8c907/href">https://medium.com/media/0d78b02e1cf6dbcf08d85cdd82f8c907/href</a></iframe><p>And our <em>big-feature.component.html</em> like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e3da50c22452dad3318ed495f0b098a/href">https://medium.com/media/7e3da50c22452dad3318ed495f0b098a/href</a></iframe><p>And to make sure we’ve been following around our <em>app.module.ts</em> should look like this so far, with the respective component declarations and the routes registered as imports.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d08c4dd12507ac5398b63938a1c262cf/href">https://medium.com/media/d08c4dd12507ac5398b63938a1c262cf/href</a></iframe><h3>4. Running our App</h3><blockquote>ng serve</blockquote><p>Now if we fire up our application, we should see this screens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*vXMSZHx9M4rPtz1y0by_WA.gif" /><figcaption>Navigating through our app.</figcaption></figure><blockquote>Nothing fancy to see here…</blockquote><p>Right now, our application bootstraps all the needed resources at boot time. Each component defined previously, is bundled in the initial package that our apps serves to the client requesting our application.</p><p>We can see this in our browser’s devtools network tab, as soon as we start our application all the bundled resources of our application are downloaded and while the user navigates through it everything is available as the application were downloaded at the startup time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*RRmGWiaAM7tImeINg3AfVA.gif" /><figcaption>Application served at startup time, with all the assets needed to work as a single page application</figcaption></figure><h3>5. Adding module lazy loading</h3><p>So far so good. Now we want to lazy load our components as soon as the user requests certain feature of our application. First, we should create a new Module, which is necessary to wrap up the components that are going to be lazy load.</p><p>The following @angular/cli command will create our new module, called big-module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c358622900ed533668e2515a0d6049b/href">https://medium.com/media/2c358622900ed533668e2515a0d6049b/href</a></iframe><p>Second, we need to adjust the folder structure to reflect the wrapping of BigFeatureComponent into our new recently created Module BigModule. So far our application folder structure should look like this, with our big-module folder wrapping big-feature-component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*y9QJWosd7jNMZm-QgK-IwQ.png" /></figure><p>Now, the interesting part about this blog post. We need to register our new Module to be lazy loaded as the user request certain feature of our app. Let’s say, when the user navigates to /bigComponent route.</p><p>For this we are gonna make use of the <em>LoadChildren </em>Module<em> </em>available in the @angular/router package. Registering a route with <em>LoadChildren </em>is as easy as the following code snippet. Here we just changed, which component we’re going to load for the children module we want to lazy load.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5645a1417fc196110774425423e4719/href">https://medium.com/media/c5645a1417fc196110774425423e4719/href</a></iframe><blockquote>The LoadChildren path should be composed by the relative path of the module without its extension + # sign + module exported class.</blockquote><p>In our case <em>‘./big-module/big-module.module#BigModuleModule’ </em>follows the latter pattern. Being <em>BigModuleModule </em>the exported class of our big-module module.</p><p>Next, we need to define the child routes of our now lazy loaded module. As we just gonna have <em>/bigComponent</em> route and not any child route after it, we just need to register the empty path as child route for our lazy loaded module followed by the component we want to load after the mentioned route is reached in our router configuration.</p><p>One interesting thing to notice here, its the use of <a href="https://angular.io/api/router/RouterModule">RouterModule</a>.forChild method. This is used because we are in a child module, being AppModule the root module of our application and is where we registered our top level application routes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60ff9d035165f8a09a7d850de162d867/href">https://medium.com/media/60ff9d035165f8a09a7d850de162d867/href</a></iframe><blockquote><em>Only call </em><a href="https://angular.io/api/router/RouterModule"><em>RouterModule</em></a><em>.forRoot in the root </em><em>AppRoutingModule (or the </em><em>AppModule if that&#39;s where you register top level application routes). In any other module, you must call the </em><a href="https://angular.io/api/router/RouterModule"><em>RouterModule</em></a><em>.forChild method to register additional routes.</em></blockquote><p>So far so good, we’ve been through a long road. That’s all we need to get our <em>BigModule</em> lazy loaded as we navigate to <em>/bigComponent </em>route<em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*LkpJPkxWccvlW5YZ1kTidg.gif" /><figcaption>big-module.module.chunk.js downloaded as we navigate /bigComponent route path.</figcaption></figure><p>We can see in the network tab of the browser’s developer tools that our app as soon as we hit the route path, a new chunk of the application is downloaded. That’s the module being <em>Lazy Loaded.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FS3Ot3hZ5bcy8o%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FS3Ot3hZ5bcy8o%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FS3Ot3hZ5bcy8o%2Fgiphy.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="339" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fbc4fc542d022c0bba6afbf91b44d59/href">https://medium.com/media/7fbc4fc542d022c0bba6afbf91b44d59/href</a></iframe><p>That’s all folks, hope you have enjoyed this post and have learned how to lazy load a module in angular. Go further, and try to optimize your application using this strategy aiming to reduce the inicial bundle size and speeding application boot time.</p><blockquote>I’m looking for a new gig. Strong experience working on highly dynamic applications using both angular and reactjs. Dms open on X formerly twitter @camilogiraldo</blockquote><blockquote>Full source code can be found in github <a href="https://github.com/camilogiraldo/lazy-load-example">here</a>.</blockquote><blockquote>Be sure to follow me <a href="https://twitter.com/camilogiraldo">@CamiloGiraldo</a> and hope to share more blog post with you guys in the future. Feel free to share. Thank you.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58fc844b5c7d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angularmedellin/lazy-loading-angular-modules-58fc844b5c7d">Lazy loading Angular Modules</a> was originally published in <a href="https://medium.com/angularmedellin">Angular Medellin</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>