<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dhananjay Kumar</title>
    <description>The latest articles on Forem by Dhananjay Kumar (@debug_mode).</description>
    <link>https://forem.com/debug_mode</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%2F212580%2F27c6e64f-b18f-40b0-bf8f-144ea101775c.png</url>
      <title>Forem: Dhananjay Kumar</title>
      <link>https://forem.com/debug_mode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/debug_mode"/>
    <language>en</language>
    <item>
      <title>Step by step, use Ocelot as an API Gateway in the .NET Core.</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Mon, 20 May 2024 03:14:05 +0000</pubDate>
      <link>https://forem.com/debug_mode/step-by-step-use-ocelot-as-an-api-gateway-in-the-net-core-47l7</link>
      <guid>https://forem.com/debug_mode/step-by-step-use-ocelot-as-an-api-gateway-in-the-net-core-47l7</guid>
      <description>&lt;p&gt;An API Gateway is a front-end server for APIs, handling incoming API requests and routing them to the appropriate backend services. It plays a crucial role in microservice architecture by offering a single entry point to the system.&lt;br&gt;
Some main functionalities of an API Gateway are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Routing &lt;/li&gt;
&lt;li&gt;Authentication &lt;/li&gt;
&lt;li&gt;Authorization &lt;/li&gt;
&lt;li&gt;Request Composition &lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Load Balancing &lt;/li&gt;
&lt;li&gt;Fault Tolerance &lt;/li&gt;
&lt;li&gt;Service Discovery &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many popular choices for API Gateway in.NET Core-based Microservices, such as, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ocelot &lt;/li&gt;
&lt;li&gt;YARP and others. 
This blog post explains how Ocelot can be an API Gateway in .NET Core APIs. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up APIs
&lt;/h2&gt;

&lt;p&gt;There are two APIs.  The first API has a ProductController with two EndPoints. &lt;/p&gt;

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

  [Route("api/products")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        static List&amp;lt;Product&amp;gt; Products = new List&amp;lt;Product&amp;gt;()
        {
            new Product { Id = 1, Name = "Product 1", Price = 10.0m },
            new Product { Id = 2, Name = "Product 2", Price = 20.0m },
            new Product { Id = 3, Name = "Product 3", Price = 30.0m }
        };
        [HttpGet]
        public async Task&amp;lt;ActionResult&amp;lt;IEnumerable&amp;lt;Product&amp;gt;&amp;gt;&amp;gt; Get()
        {
            var products = await GetProductsAsync();
            await Task.Delay(500);
            return Ok(products);
        }


        [HttpPost]
        public async Task&amp;lt;ActionResult&amp;lt;Product&amp;gt;&amp;gt; Post(Product product)
        {
            Products.Add(product);
            await Task.Delay(500);
            // Return the product along with a 201 Created status code
            return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
        }

        private Task&amp;lt;List&amp;lt;Product&amp;gt;&amp;gt; GetProductsAsync()
        {

            return Task.FromResult(Products);
        }
    }


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

&lt;/div&gt;

&lt;p&gt;These EndPoints are available at **&lt;a href="http://localhost:5047/api/products" rel="noopener noreferrer"&gt;http://localhost:5047/api/products&lt;/a&gt; **for both GET and POST operations. &lt;br&gt;
The second API has InvoiceController with just one EndPoint. &lt;/p&gt;

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

    [Route("api/invoice")]
    [ApiController]
    public class InvoiceController : ControllerBase
    {
        [HttpGet]
        public async Task&amp;lt;ActionResult&amp;lt;IEnumerable&amp;lt;string&amp;gt;&amp;gt;&amp;gt; Get()
        {
            await Task.Delay(100);
            return new string[] { "Dhananjay", "Nidhish", "Vijay","Nazim","Alpesh" };
        }
    }


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

&lt;/div&gt;

&lt;p&gt;The EndPoint is available at &lt;a href="http://localhost:5162/api/invoice" rel="noopener noreferrer"&gt;http://localhost:5162/api/invoice&lt;/a&gt; for the GET operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up API Gateway
&lt;/h2&gt;

&lt;p&gt;We are going to set up the API Gateway using Ocelot. For that, let's follow below steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an API project. &lt;/li&gt;
&lt;li&gt;Do not add any controller to that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Very first, add the Ocelot package from Nuget to the project.&lt;/p&gt;

&lt;p&gt;After adding the Ocelot package, add a file named ocelot.json to the API Gateway project. &lt;/p&gt;

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

{
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5001"
  },
  "Routes": [
    {
      "UpstreamPathTemplate": "/gateway/products",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/api/products",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5047
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/gateway/invoice",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/api/invoice",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5162
        }
      ]
    }
  ]
}


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

&lt;/div&gt;

&lt;p&gt;Let us explore each configuration in the above file. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the GlobalConfiguration section, the BaseUrl is the URL of API Gateway. API clients would interact with this URL.  When running the API Gateway project, it should run on this base URL&lt;/li&gt;
&lt;li&gt;The Routes section contains various routes in the array&lt;/li&gt;
&lt;li&gt;The Routes have UpStream and DownStream sections. &lt;/li&gt;
&lt;li&gt;The UpStream section represents the API Gateway &lt;/li&gt;
&lt;li&gt;The DownStream section represents the APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Above configuration can be depicted in below diagram:&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%2F100xz80nljo8gton48lj.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%2F100xz80nljo8gton48lj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By the above configuration, when you hit the EndPoint  &lt;a href="http://localhost:5001/gateway/products" rel="noopener noreferrer"&gt;http://localhost:5001/gateway/products&lt;/a&gt; , it would be redirected to API EndPoint &lt;a href="http://localhost:5047/api/Products" rel="noopener noreferrer"&gt;http://localhost:5047/api/Products&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Next in the Program.cs, at the App Gateway startup, add the configuration below. &lt;/p&gt;

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


builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);

app.UseOcelot();


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

&lt;/div&gt;

&lt;p&gt;Now run the API Gateway application, and you should be able to navigate the Private APIs.  Besides GET, Ocelot also supports other HTTP Verbs.  A route for POST operations can be added as shown below. &lt;/p&gt;

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

{
  "UpstreamPathTemplate": "/gateway/products",
  "UpstreamHttpMethod": [ "POST" ],
  "DownstreamPathTemplate": "/api/products",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "localhost",
      "Port": 5047
    }
  ]
},


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

&lt;/div&gt;

&lt;p&gt;Using basic configurations, you should be able to read the HttpContext object, headers, and request objects in the private API.&lt;br&gt;
 &lt;br&gt;
I hope you find this blog post helpful. Thanks for reading it.&lt;/p&gt;

</description>
      <category>dotnetcore</category>
      <category>ocelot</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Reading Request Headers Across Multiple .NET Core API Controllers</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Sat, 04 May 2024 12:47:16 +0000</pubDate>
      <link>https://forem.com/debug_mode/reading-request-headers-across-multiple-net-core-api-controllers-48a5</link>
      <guid>https://forem.com/debug_mode/reading-request-headers-across-multiple-net-core-api-controllers-48a5</guid>
      <description>&lt;p&gt;I was working on creating a .NET Core-based API and came across a requirement to read a particular request header across the API controllers.  To understand it better, let us say there are two API controllers,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;   InvoiceController
2.    ProductController&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We need to read the value of a particular Request Header in both controllers.&lt;br&gt;
 &lt;br&gt;
To do that, add a middleware class; I am naming it RequestCodeMiddleware, as we will read a request header named- &lt;strong&gt; “Request-Code.”&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;public class RequestCodeMiddleware
{
    private readonly RequestDelegate _next;
 
    public RequestCodeMiddleware(RequestDelegate next)
    {
        _next = next;
    }
 
    public async Task Invoke(HttpContext context)
    {
        // Read the request code here. This is just an example, replace it with your actual logic.
        var requestCode = context.Request.Headers["Request-Code"].FirstOrDefault();
        // Add the request code to the HttpContext
        context.Items["RequestCode"] = requestCode+"middle ware";
        // Call the next middleware in the pipeline
        await _next(context);
    }
}

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

&lt;/div&gt;



&lt;p&gt;After creating the middleware class, register it to the API handling pipeline by adding the below code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
if (app.Environment.IsDevelopment()) {     
     app.UseSwagger();     
app.UseSwaggerUI(); 
} 
app.UseMiddleware&amp;lt;RequestCodeMiddleware&amp;gt;(); app.UseHttpsRedirection(); 
app.MapControllers(); 
app.Run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can read the request header in a ProductController’s GET Action, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class product : ControllerBase
{
   
    [HttpGet]
    public string Get()
    {
        var requestCode = HttpContext.Items["RequestCode"] as string;
        return requestCode;
    }
}

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

&lt;/div&gt;



&lt;p&gt;So far, so good; however, there is still a challenge: the above code must be duplicated to read the request header in the multiple Actions. To avoid that, we can read it inside the controller’s constructor; however, &lt;strong&gt;HttpContext is unavailable inside a controller’s constructor&lt;/strong&gt;.&lt;br&gt;
 &lt;br&gt;
Another approach is to use &lt;strong&gt;IHttpContextAccesor&lt;/strong&gt;.&lt;br&gt;
 &lt;br&gt;
To use IHttpContextAccesor, register it in the API startup pipeline as shown below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddHttpContextAccessor();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in the InvoiceController's constructor, you can inject IHttpContextAccesor to read the request header, as shown in the code listing below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class invoice : ControllerBase
    {
 
        private readonly IHttpContextAccessor _httpContextAccessor;
        private string _requestCode;
 
        public invoice(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _requestCode = _httpContextAccessor.HttpContext.Items["RequestCode"] as string;
        }
 
     // Your action methods..
     // GET: api/&amp;lt;invoice&amp;gt;
        [HttpGet]
        public string Get()
        {
            return _requestCode;
        }
      }

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

&lt;/div&gt;



&lt;p&gt;In this way, we can read a request header in multiple controllers.  I hope you find this post helpful. Thanks for reading.&lt;/p&gt;

</description>
      <category>net</category>
      <category>api</category>
      <category>csharp</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting Started with Qwik</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Fri, 12 May 2023 03:09:39 +0000</pubDate>
      <link>https://forem.com/debug_mode/getting-started-with-qwik-221a</link>
      <guid>https://forem.com/debug_mode/getting-started-with-qwik-221a</guid>
      <description>&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%2Fa3vsnxkzua3tbwzf1y30.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%2Fa3vsnxkzua3tbwzf1y30.png" alt="Image description" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I write this blog post, Qwik Version 1.1 has been announced. You'll be able to learn about the announcement here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.builder.io/blog/qwik-v1" rel="noopener noreferrer"&gt;https://www.builder.io/blog/qwik-v1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Repository –  &lt;a href="https://github.com/BuilderIO/qwik" rel="noopener noreferrer"&gt;https://github.com/BuilderIO/qwik&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post does not discuss the advantages of the Qwik framework, such as Resumebility, Reactivity, etc., as there are already many discussions on the web. Instead, in this post, directly, you will jump to building and creating a fundamental QWIK app.&lt;/p&gt;

&lt;p&gt;Let us jump in. You can just run the commands below on your terminal precisely how you run any CLI commands. You must have NodeJS version 16.8 and above installed to run these commands.&lt;/p&gt;

&lt;p&gt;npm create qwik@latest&lt;/p&gt;

&lt;p&gt;Qwik will ask you specific questions, such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The app directory name.&lt;/li&gt;
&lt;li&gt;Project template. (For this Basic App with QWIKCITY)&lt;/li&gt;
&lt;li&gt;npm library installation option&lt;/li&gt;
&lt;li&gt;git init option&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Provide a name for the app, and then press enter to choose default options. After successful executions of all commands, you should get a message as shown in the below image,&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%2F9fyzo674pr3uw0ox2b0z.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%2F9fyzo674pr3uw0ox2b0z.png" alt="Image description" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, navigate to the project folder, and you should be able to see the project structure as shown in the following image,&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%2Fdc3wjk7mosj55023rj6d.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%2Fdc3wjk7mosj55023rj6d.png" alt="Image description" width="742" height="1396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the index.tsx file and replace the code with the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { component$ } from '@builder.io/qwik';

export default component$(() =&amp;gt; {
  const title = "My First QWIK App";
  return(
    &amp;lt;div&amp;gt;{title}&amp;lt;/div&amp;gt;
  )
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your first QWIK application with the command,&lt;/p&gt;

&lt;p&gt;npm run start&lt;/p&gt;

&lt;p&gt;It would be best if you got the output as shown in the below image,&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%2Foqmixd8m3278qufd6kxe.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%2Foqmixd8m3278qufd6kxe.png" alt="Image description" width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Qwik works on directory-based routing. In the routes folder, you can see a file called layout.tsx. This file has three sections,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you might have guessed correctly, the Header and Footer are two components, and in the Slot section on the route change, a component would be projected and lazy loaded.&lt;/p&gt;

&lt;p&gt;In the layout file, if you comment  and , you should get the output as shown in the following image,&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%2F7r0z9592ce300558r7wv.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%2F7r0z9592ce300558r7wv.png" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;QWIK uses QWIK CITY, a directory-based routing framework that can download minimal JavaScript with fine-grained lazy loading. Add a new directory in the routes folder to add a new route. You can create various routes by creating different subfolders, as shown below,&lt;/p&gt;

&lt;p&gt;src/routes/product&lt;/p&gt;

&lt;p&gt;src/routes/product/[id]&lt;/p&gt;

&lt;p&gt;src/routes/greet&lt;/p&gt;

&lt;p&gt;Inside the product and greet folder, add a file with the name index.tsx, and inside that file, put the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { component$ } from "@builder.io/qwik";
export default component$(()=&amp;gt;{

    return(
        &amp;lt;h1&amp;gt;Greetings&amp;lt;/h1&amp;gt;
    )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you navigate to the – &lt;a href="http://127.0.0.1:5173/greet/" rel="noopener noreferrer"&gt;http://127.0.0.1:5173/greet/&lt;/a&gt; , you should get the greet component laded as below,&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%2Faaglw20sk1w1lown3oaq.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%2Faaglw20sk1w1lown3oaq.png" alt="Image description" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you navigate to the – &lt;a href="http://127.0.0.1:5173/product/" rel="noopener noreferrer"&gt;http://127.0.0.1:5173/product/&lt;/a&gt; , you should get the product component laded as below,&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%2F13abk45e76pw2rf9lecz.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%2F13abk45e76pw2rf9lecz.png" alt="Image description" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will cover Qwik Routing in detail in other posts. However, quickly, you can create a route with a parameter. For example, to create a route with an id parameter, add a subfolder with [id] name inside the product folder and put an index inside that .tsx 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%2F9v08r1bjvtfm7ki5qi6t.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%2F9v08r1bjvtfm7ki5qi6t.png" alt="Image description" width="538" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can read the id using the useLocation hook, as shown in the next code listing:&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 "@builder.io/qwik";
import { useLocation } from "@builder.io/qwik-city";

export default component$(()=&amp;gt;{

    const loc = useLocation();
    return(
        &amp;lt;h1&amp;gt;{loc.params.id} &amp;lt;/h1&amp;gt;
    )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you navigate to – &lt;a href="http://127.0.0.1:5173/product/7/" rel="noopener noreferrer"&gt;http://127.0.0.1:5173/product/7/&lt;/a&gt;, you should get the product component laded as below,&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%2Fvh86zyxf0yr5u7ildt1x.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%2Fvh86zyxf0yr5u7ildt1x.png" alt="Image description" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another essential task in a web application is fetching data from the API. Qwik makes working with API very easy.  To fetch data from API, you can use routeLoader$, which is of type LoaderConstructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const useTodo =  routeLoader$(async ()=&amp;gt;{
    const res = await fetch('https://jsonplaceholder.typicode.com/todos/',{
        headers:{Accept:'application/json'}
    });
    return await res.json()
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use custom made useToDo hook in the component as below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default component$(()=&amp;gt;{

    const list = useTodo();
    return(
        &amp;lt;&amp;gt;
        &amp;lt;h1&amp;gt;To Do&amp;lt;/h1&amp;gt;
        &amp;lt;table&amp;gt;
            &amp;lt;thead&amp;gt;
                &amp;lt;tr&amp;gt;
                &amp;lt;th&amp;gt;Id&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Task&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Status&amp;lt;/th&amp;gt;
                &amp;lt;/tr&amp;gt;
            &amp;lt;/thead&amp;gt;
            &amp;lt;tbody&amp;gt;
            {list.value.map((item:any, index:any) =&amp;gt; (
              &amp;lt;tr key={`items-${index}`}&amp;gt; 
                 &amp;lt;td&amp;gt;{item.id} &amp;lt;/td&amp;gt;
                 &amp;lt;td&amp;gt; {item.title}&amp;lt;/td&amp;gt;
                 &amp;lt;td&amp;gt;{item.completed + ''}&amp;lt;/td&amp;gt;
             &amp;lt;/tr&amp;gt;
            ))}
            &amp;lt;/tbody&amp;gt;
        &amp;lt;/table&amp;gt;
        &amp;lt;/&amp;gt;
    )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Putting everything together in the component, you can fetch data from the API and render it in a table, as shown below:&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 "@builder.io/qwik";
import { routeLoader$ } from "@builder.io/qwik-city";

export const useTodo =  routeLoader$(async ()=&amp;gt;{
    const res = await fetch('https://jsonplaceholder.typicode.com/todos/',{
        headers:{Accept:'application/json'}
    });
    return await res.json()
})

export default component$(()=&amp;gt;{

    const list = useTodo();
    return(
        &amp;lt;&amp;gt;
        &amp;lt;h1&amp;gt;To Do&amp;lt;/h1&amp;gt;
        &amp;lt;table&amp;gt;
            &amp;lt;thead&amp;gt;
                &amp;lt;tr&amp;gt;
                &amp;lt;th&amp;gt;Id&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Task&amp;lt;/th&amp;gt;
                &amp;lt;th&amp;gt;Status&amp;lt;/th&amp;gt;
                &amp;lt;/tr&amp;gt;
            &amp;lt;/thead&amp;gt;
            &amp;lt;tbody&amp;gt;
            {list.value.map((item:any, index:any) =&amp;gt; (
              &amp;lt;tr key={`items-${index}`}&amp;gt; 
                 &amp;lt;td&amp;gt;{item.id} &amp;lt;/td&amp;gt;
                 &amp;lt;td&amp;gt; {item.title}&amp;lt;/td&amp;gt;
                 &amp;lt;td&amp;gt;{item.completed + ''}&amp;lt;/td&amp;gt;
             &amp;lt;/tr&amp;gt;
            ))}
            &amp;lt;/tbody&amp;gt;
        &amp;lt;/table&amp;gt;
        &amp;lt;/&amp;gt;
    )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see the data rendered in a table as shown below:&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%2F7lvoxlvhuswnlvqzi1ja.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%2F7lvoxlvhuswnlvqzi1ja.png" alt="Image description" width="800" height="750"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As with other frameworks, Components are the basic building blocks of Qwik.  They are a reusable piece of code. In Qwik, a component is a function that returns a JSX.  You can create a component,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By adding a folder with the name of the component&lt;/li&gt;
&lt;li&gt;Inside that, add a file with a name index or component name.&lt;/li&gt;
&lt;li&gt;Add style with the component.module.css file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you wish to create a greet component, add a folder greet; inside that, add an index.tsx or greet.tsx&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%2F0uyw2ua6hxzrmv61aj86.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%2F0uyw2ua6hxzrmv61aj86.png" alt="Image description" width="614" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Greet component, you can apply the CSS from the greet.module.css file as shown in the next code listing:&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 "@builder.io/qwik"
import styles from './greet.module.css';

export default component$(() =&amp;gt; {

    const title = "Greeting "

    return (
        &amp;lt;&amp;gt;
           &amp;lt;div class={styles['abc']} &amp;gt;{title}&amp;lt;/div&amp;gt;
        &amp;lt;/&amp;gt;
    )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you can use the Greet component on Index as below,&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 '@builder.io/qwik';
import Greet from '../components/greet/greet'

export default component$(() =&amp;gt; {
  const title = "My First QWIK App";
  return(
    &amp;lt;&amp;gt;
    &amp;lt;div&amp;gt;{title}&amp;lt;/div&amp;gt;
    &amp;lt;Greet/&amp;gt;
    &amp;lt;/&amp;gt;
  )
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some characteristics of  Qwik components are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Qwik components get broken down into lazy-loaded chunks&lt;/li&gt;
&lt;li&gt;Qwik components are resumable&lt;/li&gt;
&lt;li&gt;Qwik components are reactive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will cover more about the components and other concepts of QWIK in the following posts. I hope you will like working with QWIK and like this starter post. Thanks for reading it.&lt;/p&gt;

</description>
      <category>qwik</category>
      <category>webdev</category>
      <category>qwikdev</category>
    </item>
    <item>
      <title>Hello Angular 16</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Sat, 06 May 2023 05:55:21 +0000</pubDate>
      <link>https://forem.com/debug_mode/hello-angular-16-3km7</link>
      <guid>https://forem.com/debug_mode/hello-angular-16-3km7</guid>
      <description>&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%2Fq7b0ld9c1996321ib55n.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%2Fq7b0ld9c1996321ib55n.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On 3rd May 2023, Angular 16 is launched.  Some of the essential features of Angular 16 are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improved standalone APIs&lt;/li&gt;
&lt;li&gt;Angular Signals&lt;/li&gt;
&lt;li&gt;takeUntillDestrory RxJS operator&lt;/li&gt;
&lt;li&gt;DestroyRef as an injectable provider&lt;/li&gt;
&lt;li&gt;Required input properties for directives&lt;/li&gt;
&lt;li&gt;Passing router data as a component input&lt;/li&gt;
&lt;li&gt;Self-closing tags&lt;/li&gt;
&lt;li&gt;ES Build support&lt;/li&gt;
&lt;li&gt;Nondestructive hydration for SSR&lt;/li&gt;
&lt;li&gt;Unit Testing with Jest&lt;/li&gt;
&lt;li&gt;Support for TypeScript 5.0&lt;/li&gt;
&lt;li&gt;ngTemplateOutlet strick type checking&lt;/li&gt;
&lt;li&gt;Configure zonejs (ngZone) in bootstrapApplication and many more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read the official announcement here: &lt;a href="https://blog.angular.io/angular-v16-is-here-4d7a28ec680d" rel="noopener noreferrer"&gt;https://blog.angular.io/angular-v16-is-here-4d7a28ec680d&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To play around, Let us start with updating to Angular 16.&lt;/p&gt;

&lt;p&gt;**npm uninstall -g @angular/cli&lt;/p&gt;

&lt;p&gt;npm install -g @angular/cli@latest**&lt;/p&gt;

&lt;p&gt;After upgrading to Angular 16, you can create a Standalone Angular application using the CLI Command,&lt;/p&gt;

&lt;p&gt;ng new myapp –standalone&lt;/p&gt;

&lt;p&gt;In the new project structure, you can see that there are no modules, and there is one new file named app.config.ts&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%2Fnkebbpbxr0nomjy52kpm.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%2Fnkebbpbxr0nomjy52kpm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The app.config file contains the providers that should be available to the root component and all its children in a standalone Angular application.&lt;/p&gt;

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

import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';

import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes) ]
};


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

&lt;/div&gt;

&lt;p&gt;In the main.ts file, you can see that application is bootstrapped using AppComponent and the configuration from appConfig&lt;/p&gt;

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

bootstrapApplication(AppComponent, appConfig)
  .catch((err) =&amp;gt; console.error(err));


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

&lt;/div&gt;

&lt;p&gt;Here AppComponent is a standalone component used as a root component to bootstrap the application.&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';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'myapp';
}


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

&lt;/div&gt;

&lt;p&gt;You can notice in the AppComponent,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its standalone property is set to true&lt;/li&gt;
&lt;li&gt;It’s all dependencies are passed inside the imports array
You can add new components to the application using the ng g c name command. Since the application is configured for standalone API, the newly created component would be a standalone component. Add a new component in the application,&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;ng g c product&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To use ProductComponent inside the AppComponent, add it as a dependency in the imports array.&lt;/p&gt;

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

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, 
    RouterOutlet, 
    ProductComponent],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'myapp';
}


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

&lt;/div&gt;

&lt;p&gt;If a standalone component depends on a module, you pass that module in the imports array of the component.  For example, in the ProductComponent, you can pass ReactiveFormsModule as a dependency, as shown in the following code block,&lt;/p&gt;

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

@Component({
  selector: 'app-product',
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule],
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent {

  fb = inject(FormBuilder);
  login : FormGroup; 
  constructor(){
    this.login = this.fb.group({
      email:[],
      password:[]
    })
  }
}


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

&lt;/div&gt;

&lt;p&gt;I have written a detailed article on standalone components. For deep dive, read it here –&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.telerik.com/blogs/angular-14-introducing-standalone-components" rel="noopener noreferrer"&gt;https://www.telerik.com/blogs/angular-14-introducing-standalone-components&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As of now, you have created an Angular 16 app configured to work with standalone APIs. As discussed, Angular 16 comes with many exciting features, and one such feature is compile time required input check or required @Input properties to a component&lt;/p&gt;

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

export class ProductComponent {

  @Input({required:true}) message?: string; 


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

&lt;/div&gt;

&lt;p&gt;While using this component, Angular complains about if you do not pass the message property value.&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%2Fobawzqvs9mg3bq1tffex.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%2Fobawzqvs9mg3bq1tffex.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Required input implies that the directive can’t work without them.  Read more about required @Input properties here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://debugmode.net/2023/04/17/required-input-properties-in-angular/" rel="noopener noreferrer"&gt;https://debugmode.net/2023/04/17/required-input-properties-in-angular/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another exciting feature of Angular 16 is fine-grained reactivity with the signal.  A signal is a function to create reactive values.&lt;/p&gt;

&lt;p&gt;You can create a signal as shown below.&lt;/p&gt;

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

count = 1; 
  counter = signal(this.count); 

  constructor(){
    effect(()=&amp;gt;{
      console.log(this.counter())
    })
  }

  setCount():void{
    this.counter.update(count =&amp;gt; count + 1);
  }


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

&lt;/div&gt;

&lt;p&gt;You can learn more about signals here –&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/angular/angular/discussions/49683" rel="noopener noreferrer"&gt;https://github.com/angular/angular/discussions/49683&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Angular 16 has many exciting features, new enhancements, and capabilities to write a scalable, performant Angular app. In further posts, I will write more details about Angular 16 features. I hope you like this blog post. Thanks for reading.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>angular16</category>
    </item>
    <item>
      <title>What is the purpose of the Second Parameter in creating an Angular Signal</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Sat, 06 May 2023 02:59:39 +0000</pubDate>
      <link>https://forem.com/debug_mode/what-is-the-purpose-of-the-second-parameter-in-creating-an-angular-signal-5g61</link>
      <guid>https://forem.com/debug_mode/what-is-the-purpose-of-the-second-parameter-in-creating-an-angular-signal-5g61</guid>
      <description>&lt;p&gt;The angular signal() function creates a signal. This function takes two input parameters.  &lt;/p&gt;

&lt;p&gt;Initial value&lt;br&gt;
Optional Equality function.&lt;br&gt;
By default, Angular executes the code of effect() even if the signal is set or updated with the same value. How can we avoid that?&lt;/p&gt;

&lt;p&gt;By leveraging the second parameter of the signal() function.&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%2F7tuhn6yn7vv1cizmq5c1.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%2F7tuhn6yn7vv1cizmq5c1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us first understand the problem statement through an example.&lt;/p&gt;

&lt;p&gt;Angular provides the signal function to create a signal. You can create a signal as shown next,&lt;/p&gt;

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

product : IProduct = {
    Id:  "1",
    Title:"Pen",
    inStock: true,
    Price : 100

  }
  Product$ = signal&amp;lt;IProduct&amp;gt;(this.product);


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

&lt;/div&gt;

&lt;p&gt;You can use the Product$ signal by calling it a function on the template.&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;{{Product$() | json}}&amp;lt;/h2&amp;gt;
&amp;lt;button (click)="setProduct()"&amp;gt;Set Products&amp;lt;/button&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;An angular signal provides a set method to replace its value with the new value. This method removes the older value and sets the new value in the signal. In the setProduct function, update the signal with a new value as shown below,&lt;/p&gt;

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

setProduct(){
   let p : IProduct = 
      {
        Id:"1",
        Title:"Pen",
        Price: 300,
        inStock: true
      }; 
    this.Product$.set(p);
  }


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

&lt;/div&gt;

&lt;p&gt;As you see, calling the setProduct function sets the signal with the same product each time.  &lt;/p&gt;

&lt;p&gt;Angular Signal has another concept called Effect.  Angular calls the effect() function each time the signal changes or emits a new value. So let us say you need to run a set of code; when the signal changes, you put that code in effect.&lt;/p&gt;

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

  productEffect = effect(()=&amp;gt;{
     console.log('effect called'); 
     console.log(this.Product$());
  });


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

&lt;/div&gt;

&lt;p&gt;In the productEffect effect(), we are printing the value of the Product$ signal. You may observe that,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each time you click the button, the signal changes and is set with the same product.&lt;/li&gt;
&lt;li&gt;It is the same product set to the signal, but the signal emits it.&lt;/li&gt;
&lt;li&gt;Each time you click the button, the code in effect() is executed even though the signal emits the same product value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each time you click the button, the code in effect() is executed even though the signal emits the same product value.  THIS IS THE PROBLEM, and it can be fixed by passing a second parameter in the signal function() while creating the signal.&lt;/p&gt;

&lt;p&gt;The signal function takes two input parameters,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initial value&lt;/li&gt;
&lt;li&gt;Optional Equality function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The signal function is declared as below,&lt;/p&gt;

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

export declare function signal&amp;lt;T&amp;gt;(initialValue: T, options?: CreateSignalOptions&amp;lt;T&amp;gt;): WritableSignal&amp;lt;T&amp;gt;;


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

&lt;/div&gt;

&lt;p&gt;As you see that you can pass the options. The type of options is CreateSignalOptions, which is defined below,&lt;/p&gt;

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

export declare interface CreateSignalOptions&amp;lt;T&amp;gt; {
    /**
     * A comparison function which defines equality for signal values.
     */
    equal?: ValueEqualityFn&amp;lt;T&amp;gt;;
}


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

&lt;/div&gt;

&lt;p&gt;The CreateSignalOptions has a method named equal. You can pass this equal function as the second parameter. The function can write logic to determine when the signal notifies and emits.&lt;/p&gt;

&lt;p&gt;You can create the equal function as shown in the following code block:&lt;/p&gt;

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

function signalEquality(a:IProduct, b: IProduct) {
  console.log("hello") // it executes each time
  if(JSON.stringify(a)=== JSON.stringify(b)){
    return true;
  }
  return false; 
}


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

&lt;/div&gt;

&lt;p&gt;You can pass the above function as the value of the equal function while creating the signal.&lt;/p&gt;

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

 Product$ = signal&amp;lt;IProduct&amp;gt;(this.product,{equal:signalEquality});



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

&lt;/div&gt;

&lt;p&gt;Now each time you click the button, the code in effect() is only executed when the signal() is set with a new value and emitting it.&lt;/p&gt;

&lt;p&gt;I hope you understand the purpose of the second parameter in creating a signal in Angular. Thanks for reading.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Interface Segregation Principle in TypeScript</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Fri, 28 Apr 2023 09:07:09 +0000</pubDate>
      <link>https://forem.com/debug_mode/interface-segregation-principle-in-typescript-49g5</link>
      <guid>https://forem.com/debug_mode/interface-segregation-principle-in-typescript-49g5</guid>
      <description>&lt;p&gt;The interface Segregation Principle is one of the fundamental principles of the SOLID principle. It says to keep the interface as small as possible. In the words of &lt;strong&gt;Uncle Bob&lt;/strong&gt;, &lt;em&gt;Keep interfaces small so that classes don’t end up depending on things they don’t need&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;To understand the problem, say there is an interface IVehicle with two methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export interface IVehicle{
    drive():void; 
    fly():void; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two classes, Car and Plane, implement the IVehicle interface. The Car class uses the interface as shown in the next code block,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { IVehicle } from "./interface/vehicle.interface";
 
export class Car implements IVehicle{
 
    drive(): void {
        console.log('can drive');
    }
    fly(): void {
        throw new Error('Method is not implemented.'); 
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since a car cannot fly, the Car class is forced to implement the fly method, and for that, in the TypeScript, we are throwing an error with the message that Method is not implemented. &lt;/p&gt;

&lt;p&gt;The Plane class uses the interface as shown in the next code block,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { IVehicle } from "./interface/vehicle.interface";

export class Plane implements IVehicle{
 
    drive(): void {
        throw new Error("Method is not implemented.");
    }
 
    fly(): void {
        console.log('you can fly a plane')
    }
 
}

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

&lt;/div&gt;



&lt;p&gt;Since a plane cannot drive, the Plane class is forced to implement the drive method, and for that, in the TypeScript, we are throwing an error with the message that Method is not implemented. &lt;br&gt;
 &lt;br&gt;
The above implementation violates SOLID’s ISP principle.  &lt;/p&gt;

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

&lt;p&gt;To adhere to the Interface Segregation Principle, refactor the IVehicle interface in two different interfaces.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ICar interface &lt;/li&gt;
&lt;li&gt;IPlane interface
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export interface ICar{
    drive():void;  
}

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

&lt;/div&gt;


&lt;p&gt;The ICar interface contains only the drive function, and the IPlane interface includes only the fly function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export interface IPlane{
    fly():void; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Car and Plane classes implement these two interfaces ICar and IPlane, respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ICar } from "./interface/car.interface";
 
export class Car implements ICar{
 
    drive(): void {
        console.log('you can drive a car');
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the Plane class implements the IPlane interface,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { IPlane } from "./interface/plane.interface";
 
export class Plane implements IPlane{
 
    fly(): void {
        console.log('you can fly a plane')
    }
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this implementation, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interface is small&lt;/li&gt;
&lt;li&gt;The class that implements the interface is not forced to implement any method it does not need. &lt;/li&gt;
&lt;li&gt;The class is not violating the Single Responsibility Principle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After refactoring to the Interface Segregation Principle, interfaces and classes are in a one-to-one relationship and adhere to the Single Responsibility Principle. &lt;/p&gt;

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

&lt;p&gt;If there is a super vehicle that flies and drives both, that can implement both interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ICar } from "./interface/car.interface";
import { IPlane } from "./interface/plane.interface";
 
export class SuperVehicle implements ICar,IPlane {
 
    fly(): void {
        console.log('can fly');
    }
    drive(): void {
        console.log('can drive'); 
    }
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above implementation,  as we have created specific interfaces, classes never implement the interfaces it does not need. &lt;br&gt;
 &lt;br&gt;
I strongly recommend using SOLID principles while working with TypeScript in Angular or node applications. I hope you find this post helpful. Thanks for reading. &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>solid</category>
    </item>
    <item>
      <title>How to check the status of an Angular Reactive Form’s Control</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Wed, 26 Apr 2023 03:51:37 +0000</pubDate>
      <link>https://forem.com/debug_mode/how-to-check-the-status-of-an-angular-reactive-forms-control-2387</link>
      <guid>https://forem.com/debug_mode/how-to-check-the-status-of-an-angular-reactive-forms-control-2387</guid>
      <description>&lt;p&gt;Sometimes, you may have a requirement to do something when the status of an Angular form control changes.  For example, you have a Login Form, and when the status of the Email control changes from valid to invalid and vice versa, you need to perform some actions. In this post, let us see how we can achieve that. &lt;br&gt;
 &lt;br&gt;
We have a login form,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fb = inject(FormBuilder);
loginForm: FormGroup;

  constructor() {

    this.loginForm = this.fb.group({
      email: [null, [Validators.required]],
      password: [null, [Validators.required]],
    })

 }

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

&lt;/div&gt;



&lt;p&gt;On the template, map the loginForm to the HTML Form element and FormControls to the input elements.&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;form (ngSubmit)="login()" [formGroup]="loginForm" novalidate&amp;gt;
&amp;lt;input type="text" formControlName="email" placeholder="Enter email" /&amp;gt;
&amp;lt;br/&amp;gt;
&amp;lt;input type="password" formControlName="password" placeholder="Enter Password"/&amp;gt;
&amp;lt;br/&amp;gt;
&amp;lt;br/&amp;gt;
&amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever the status of the email field changes, you need to take some action.  For that, Angular AbstractControlDirective provides a getter of an observable type called statusChanges().&lt;/p&gt;

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

&lt;p&gt;You can subscribe to the statusChanges() observable to work with the control’s status change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;checkControlStatus(){
    const email = this.loginForm.get('email'); 
    email?.statusChanges.subscribe((status:any)=&amp;gt;{
       console.log(status);
    })
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And further, call the above function In the ngOnInit() life cycle hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngOnInit(): void {
    this.checkControlStatus();
  }

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

&lt;/div&gt;



&lt;p&gt;You can check the status for valid or invalid and apply conditional css or log to the API etc. I hope you find this post helpful. Thanks for reading. &lt;/p&gt;

</description>
      <category>angular</category>
    </item>
    <item>
      <title>How to Lazy and Dynamically Load a Component in Angular</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Fri, 21 Apr 2023 04:53:26 +0000</pubDate>
      <link>https://forem.com/debug_mode/how-to-lazy-and-dynamically-load-a-component-in-angular-18ke</link>
      <guid>https://forem.com/debug_mode/how-to-lazy-and-dynamically-load-a-component-in-angular-18ke</guid>
      <description>&lt;p&gt;There are three ways you can use a component in an Angular app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loading on route change &lt;/li&gt;
&lt;li&gt;Using as a child component &lt;/li&gt;
&lt;li&gt;Dynamically loading on demand &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, this article focuses on loading a component dynamically and lazily. The main advantage of lazily loading a component is reducing the initial bundle size and only downloading the component in the browser when required. &lt;br&gt;
 &lt;br&gt;
Let us say that you have a component called GreetComponent, as shown in the next code block,&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, EventEmitter, Input, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
 
const template = `
   &amp;lt;h2&amp;gt;{{message}} &amp;lt;/h2&amp;gt;
   &amp;lt;button (click)='sendMessage()'&amp;gt;Send Message &amp;lt;/button&amp;gt;
`
@Component({
  selector: 'app-greet',
  standalone: true,
  imports: [CommonModule],
  template: template
})
export class GreetComponent {
  @Input({ required: true }) message?: string;
  @Output() messageEvent = new EventEmitter&amp;lt;boolean&amp;gt;();
  sendMessage(): void {
    this.messageEvent.emit(true);
  }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The GreetComponent has an @Input decorated property and an @Output() decorated EvenEmmiter. The FooComponent uses it as a child component, as shown in the following code block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const template = `
   &amp;lt;app-greet [message]='message' (messageEvent)='sendMessage($event)'&amp;gt;&amp;lt;/app-greet&amp;gt;
`
@Component({
  selector: 'app-foo',
  standalone: true,
  imports: [CommonModule,
    GreetComponent],
  template : template
})
export class FooComponent {
   message = "data from parent"
 
   sendMessage(m:boolean){
      console.log(m);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A couple of points worth noticing here are, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GreetComponent is part of the imports array.&lt;/li&gt;
&lt;li&gt;GreetComponent is used on the template. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to the above two points, whenever Angular compiles FooComponent, it also includes GreetComponent, increasing the size of the bundle containing the FooComponent. &lt;br&gt;
 &lt;br&gt;
 &lt;br&gt;
*&lt;em&gt;One main advantage of loading a component dynamically (lazily) is that it reduces the bundle size because it only gets downloaded in the browser when required. *&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
 &lt;br&gt;
Let us say that,  GreetComponent should be loaded dynamically and lazily with the click of a button in the FooComponent. For that,  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a button &lt;/li&gt;
&lt;li&gt;Remove  from the template of FooComponent&lt;/li&gt;
&lt;li&gt;Remove GreetComponent from the imports array. [Important]
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const template = `
   &amp;lt;button (click)='loadComponent()'&amp;gt;Load Greet Component &amp;lt;/button&amp;gt;
`
@Component({
  selector: 'app-foo',
  standalone: true,
  imports: [CommonModule],
  template: template
})
export class FooComponent {
  message = "data from parent"
  greetcomp: any;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To dynamically load the component, inject the ViewContainerRef using the inject function or the constructor injection.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;vcr = inject(ViewContainerRef);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, import the file that contains GreetComponent using the import statement.  The import statement is used in JavaScript to load a file dynamically. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const { GreetComponent } = await import('../greet/greet.component');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After importing the file, use the CreateComponent method of ViewContainerRef.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.greetcomp = this.vcr.createComponent(GreetComponent);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can access all properties and events of the dynamically loaded components using the instance method. So the a value can be passed to the message property as shown in the next code block, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.greetcomp.instance.message = "Hello dynamic Component";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can subscribe to the @Output decorated EventEmitter as shown next,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.greetcomp.instance.messageEvent.subscribe((data:any)=&amp;gt;{
        console.log(data);
      })

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

&lt;/div&gt;



&lt;p&gt;Putting everything together, you can lazy load a GreetComponent with the click of a button in the FooComponent, as shown in the following code listing,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const template = `
   &amp;lt;button (click)='loadComponent()'&amp;gt;Load Greet Component &amp;lt;/button&amp;gt;
`
@Component({
  selector: 'app-foo',
  standalone: true,
  imports: [CommonModule],
  template: template
})
export class FooComponent {
  message = "data from parent"
  greetcomp: any;
  vcr = inject(ViewContainerRef);
  
  async loadComponent() {
    this.vcr.clear();
    const { GreetComponent } = await import('../greet/greet.component');
    this.greetcomp = this.vcr.createComponent(GreetComponent);
    if (this.greetcomp) {
      this.greetcomp.instance.message = "Hello dynamic Component";
 
      this.greetcomp.instance.messageEvent.subscribe((data:any)=&amp;gt;{
        console.log(data);
      })
    }
 
  }
}

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

&lt;/div&gt;



&lt;p&gt;Currently, Angular loads GreetComponent at the end after all DOM elements of FooComponent. To load it at a specific div block, read the div block as ViewChild and ViewConatinerRef.  In the other post, I will cover that in detail. &lt;br&gt;
 &lt;br&gt;
I hope you find this post helpful. Thanks for reading. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular 16 REQUIRED Input Properties</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Tue, 18 Apr 2023 04:13:38 +0000</pubDate>
      <link>https://forem.com/debug_mode/angular-16-required-input-properties-4j90</link>
      <guid>https://forem.com/debug_mode/angular-16-required-input-properties-4j90</guid>
      <description>&lt;p&gt;Angular 16 adds a new feature of making an @Input() decorated property REQUIRED, which means that to use that component, the value of the required property has to be passed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Input({required:true}) title? :string; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To Understand it, let us assume that you have a component named ProductComponent with @Input() decorated properties as shown below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const template = `
   &amp;lt;p&amp;gt;title : {{title}}  price : {{price}} &amp;lt;/p&amp;gt;  
`
@Component({
  selector: 'app-product',
  standalone: true,
  imports: [CommonModule],
  template : template
})
export class ProductComponent {
 
  @Input() title? :string; 
  @Input() price? : string; 
 
}

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

&lt;/div&gt;



&lt;p&gt;We are using ProductComponent inside AppComponent, as shown below.&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';
 
const template = `
&amp;lt;app-product [title]="title"&amp;gt;&amp;lt;/app-product&amp;gt;
`
@Component({
  selector: 'app-root',
  template : template
})
export class AppComponent {
  title = "Pen"; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you notice here is that we are, &lt;br&gt;
 &lt;br&gt;
·      Passing value to the title property&lt;br&gt;
·      Not passing value to the price property. &lt;br&gt;
 &lt;br&gt;
In the output, you can see that value of the title is printed, whereas the value of the price is rendered empty. &lt;/p&gt;

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

&lt;p&gt;We can use the ProductComponent without passing the value of all input decorated properties, and Angular does not complain about that.  Now let us say that we have a requirement that, &lt;br&gt;
 &lt;br&gt;
*&lt;em&gt;The price property is a required property to use the ProductComponent. *&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
Starting Angular 16, we can make an @Input() decorated property as the required property. As you see Input interface has an optional, required boolean property. &lt;/p&gt;

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

&lt;p&gt;We can use the required property to make an @Input() decorated property required, as shown below, &lt;/p&gt;

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

&lt;p&gt;You can create a component with a combination of required and optional properties, as shown below,&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 } from '@angular/core';
import { CommonModule } from '@angular/common';
 
const template = `
   &amp;lt;p&amp;gt;title : {{title}}  price : {{price}} &amp;lt;/p&amp;gt;  
`
@Component({
  selector: 'app-product',
  standalone: true,
  imports: [CommonModule],
  template : template
})
export class ProductComponent {
 
  @Input() title? :string; 
  @Input({required:true}) price? : string; 
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Angular complains about when you try using the ProductComponent without passing value for price property. &lt;/p&gt;

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

&lt;p&gt;To use the ProductComponent, you must pass the value of the required @Input() decorated property as shown in the next code block,&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';
 
const template = `
&amp;lt;app-product [title]="title" [price]="price"&amp;gt;&amp;lt;/app-product&amp;gt;
`
@Component({
  selector: 'app-root',
  template : template
})
export class AppComponent {
  title = "Pen"; 
  price = "100"; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, you can make an @Input() decorated property as required. I hope you find this post helpful. Thanks for reading. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The simplest way to disable console.log for Production build in Angular?</title>
      <dc:creator>Dhananjay Kumar</dc:creator>
      <pubDate>Wed, 06 Apr 2022 06:02:34 +0000</pubDate>
      <link>https://forem.com/debug_mode/the-simplest-way-to-disable-consolelog-for-production-build-in-angular-1h1f</link>
      <guid>https://forem.com/debug_mode/the-simplest-way-to-disable-consolelog-for-production-build-in-angular-1h1f</guid>
      <description>&lt;p&gt;I work with different developers, and most of them rely on console.log () or other console methods for development. They use the console to print various values to debug while doing the development. I call this approach console-driven development. Cool name, right? &lt;/p&gt;

&lt;p&gt;These console functions become overwhelming in production as users can see many things printed in the browser console, which they may or may not understand. Sometimes a junior developer can leave something significant on the console that may cause a potential security risk. &lt;/p&gt;

&lt;p&gt;So, we must not have the console in the productions. There are various ways you can disable console in the production, such as: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;By using libraries such as logger &lt;/li&gt;
&lt;li&gt;Creating a linting rule that if code contains any console function, the build fails &lt;/li&gt;
&lt;li&gt;Or you are writing a simple service to perform this task for you.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this post, let us see how we can create service to disable the console in the production step-by-step.&lt;/p&gt;

&lt;p&gt;Add service to the project. I am calling it as ConsoleToogleService. In the service, we make all the console functions void. So service should look like the below:&lt;br&gt;
&lt;/p&gt;

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

@Injectable({
  providedIn: 'root'
})
export class ConsoleToggleService {

  constructor() { 

  }

  disableConsoleInProduction(): void {
    if(environment.production){
      console.warn(`🚨 Console output is disabled on production!`);
      console.log = function():void{}; 
      console.debug = function():void{};
      console.warn = function():void{};
      console.info = function():void{};
    }
  }
}

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

&lt;/div&gt;



&lt;p&gt;In the service, you can see a function called disableConsoleInProduction(). Inside this function, we are making all the console functions void. It is as simple as this. &lt;/p&gt;

&lt;p&gt;Now in the AppComponent, inject this service and call the function in the constructor as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  constructor(private consoleToggleService : ConsoleToggleService){

    this.consoleToggleService.disableConsoleInProduction();

  }

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

&lt;/div&gt;



&lt;p&gt;When you build an application for production, you won’t have any console. However, the development console will work as usual. &lt;br&gt;
I hope you find this post helpful. Thanks for reading. &lt;/p&gt;

</description>
      <category>angular</category>
    </item>
  </channel>
</rss>
