<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cagkan Mert Oztas</title>
    <description>The latest articles on DEV Community by Cagkan Mert Oztas (@cagkanmert).</description>
    <link>https://dev.to/cagkanmert</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%2F1013119%2F01e8bc22-6606-4d47-99a8-1a2a5a9de31d.jpg</url>
      <title>DEV Community: Cagkan Mert Oztas</title>
      <link>https://dev.to/cagkanmert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cagkanmert"/>
    <language>en</language>
    <item>
      <title>Angular InjectionToken: Dependency Injection In-Depth</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Tue, 21 Feb 2023 20:37:20 +0000</pubDate>
      <link>https://dev.to/cagkanmert/angular-injectiontoken-dependency-injection-in-depth-2kfo</link>
      <guid>https://dev.to/cagkanmert/angular-injectiontoken-dependency-injection-in-depth-2kfo</guid>
      <description>&lt;p&gt;At first, I would like to thank &lt;strong&gt;&lt;a href="https://www.linkedin.com/in/%C3%B6merincirku%C5%9F/"&gt;Omer Incirkus&lt;/a&gt;&lt;/strong&gt;, who helped me write this article and was with me while I was learning, for his contributions.&lt;/p&gt;




&lt;p&gt;Dependency injection is a crucial concept in Angular that enables you to create &lt;strong&gt;loosely coupled&lt;/strong&gt; components, services, and modules. With dependency injection, you can declare a service or component once and reuse it throughout your application, which makes your code more modular, testable, and maintainable.&lt;/p&gt;

&lt;p&gt;Angular provides different ways to inject dependencies into your components, such as constructor injection. However, sometimes you may want to inject a value that is not a class or a service instance but rather a simple configuration value, such as a string, number, or boolean. In this case, you can use the &lt;strong&gt;InjectionToken&lt;/strong&gt; class to define a token that represents the value you want to inject.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an InjectionToken in Angular?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An InjectionToken is a class in Angular that represents a unique identifier for a dependency.&lt;/strong&gt; It provides a way to decouple the provider and the consumer of a dependency by allowing you to register a token with a provider and then inject the token into a consumer. The consumer can then use the token to retrieve the value of the dependency from the provider.&lt;/p&gt;

&lt;p&gt;The InjectionToken class has a generic type parameter that specifies the type of the value that the token represents. For example, if you want to inject a string value, you can define an InjectionToken with a string type parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MY_STRING_TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-string-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define an InjectionToken named ‘&lt;strong&gt;MY_STRING_TOKEN&lt;/strong&gt;’ with a string type parameter. The first argument of the InjectionToken constructor is a string that provides a human-readable description of the token. &lt;em&gt;This is optional, &lt;strong&gt;but recommended&lt;/strong&gt;, as it helps with debugging and error reporting.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use an InjectionToken in Angular?
&lt;/h2&gt;

&lt;p&gt;To use an InjectionToken, you need to register it as a provider in the module or component where you want to use it. You can do this by defining a provider with the token and the value that you want to inject:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MY_STRING_TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-string-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-my-component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;h1&amp;gt;{{title}}&amp;lt;/h1&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MY_STRING_TOKEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;useValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MY_STRING_TOKEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario; &lt;strong&gt;If you don’t specify InjectionToken in provider, you will get R3InjectorError&lt;/strong&gt;. If you want to avoid error and null injection error even though you didn’t specify it in providers, you should write InjectorToken like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MY_STRING_TOKEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-string-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;providedIn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// root, any or any Component or Module&lt;/span&gt;
  &lt;span class="na"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// The default value of token&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in this example (the above component), we declare a component named ‘MyComponent’ and &lt;strong&gt;register the ‘MY_STRING_TOKEN’ token&lt;/strong&gt; as a provider with a string value of ‘Hello, world!’. We use the ‘&lt;strong&gt;@Inject&lt;/strong&gt;‘ decorator to inject the value of the ‘MY_STRING_TOKEN’ token into the component’s constructor.&lt;/p&gt;

&lt;p&gt;When the component is instantiated, Angular uses the ‘MY_STRING_TOKEN’ token to retrieve the string value from the provider and inject it into the component’s constructor. The component can then use the injected value as needed.&lt;/p&gt;

&lt;p&gt;You can also use InjectionToken to inject complex objects into your components, services, or modules. For example, you can define an InjectionToken that represents a configuration object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;AppConfig&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;APP_CONFIG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InjectionToken&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;AppConfig&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-config&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AppConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;APP_CONFIG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;useValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define an interface ‘AppConfig’ that describes a configuration object with two properties then it used on AppModule providers.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;a href="https://cagkanmert.com"&gt;Check out my websites for more...&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>injectiontoken</category>
      <category>dependencyinjection</category>
      <category>r3injectorerror</category>
    </item>
    <item>
      <title>Angular Change Detection: Understanding It and Best Practices</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Thu, 16 Feb 2023 19:55:15 +0000</pubDate>
      <link>https://dev.to/cagkanmert/angular-change-detection-understanding-it-and-best-practices-3lm9</link>
      <guid>https://dev.to/cagkanmert/angular-change-detection-understanding-it-and-best-practices-3lm9</guid>
      <description>&lt;p&gt;Angular is a widely-used framework for building web applications, and its Change Detection system is a key feature. In this blog, we’ll discuss the purpose of Angular Change Detection, how it works, and best practices for optimizing its performance. Understanding these concepts will help you create efficient and fast Angular applications that provide a seamless user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Change Detection in Angular?
&lt;/h2&gt;

&lt;p&gt;Angular Change Detection is a process that tracks changes to an application’s data and updates the UI accordingly. Whenever there is a change in the data, the Change Detection system is triggered to determine &lt;strong&gt;if the change requires an update to the UI&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Angular Change Detection work?
&lt;/h2&gt;

&lt;p&gt;Angular’s Change Detection system is &lt;strong&gt;based on the Zone.js library&lt;/strong&gt;, which creates a root Zone that tracks all changes to the application’s state. Each component in the application has its own Change Detection strategy that determines how it should handle changes to its data. There are two types of Change Detection strategies in Angular: OnPush and Default. &lt;strong&gt;OnPush&lt;/strong&gt; is recommended for &lt;em&gt;static components&lt;/em&gt;, while &lt;strong&gt;Default&lt;/strong&gt; is recommended for &lt;em&gt;components with frequent updates&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To optimize Angular Change Detection performance depends on best practices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the OnPush Change Detection strategy wherever possible.&lt;/li&gt;
&lt;li&gt;Avoid using complex and deeply-nested data structures.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;trackBy&lt;/strong&gt; function when using *ngFor directives.&lt;/li&gt;
&lt;li&gt;Use immutable data structures whenever possible.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;ChangeDetectorRef&lt;/strong&gt; class to manually trigger the Change Detection process when necessary.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;a href="https://cagkanmert.com"&gt;Check out my website for more…&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>changedetection</category>
      <category>performanceoptimization</category>
      <category>zonejs</category>
    </item>
    <item>
      <title>The ContentAfterCheck Hook in Angular</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Thu, 02 Feb 2023 09:48:31 +0000</pubDate>
      <link>https://dev.to/cagkanmert/the-contentaftercheck-hook-in-angular-2kh9</link>
      <guid>https://dev.to/cagkanmert/the-contentaftercheck-hook-in-angular-2kh9</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fn85fqrnn0jfm1a05di2e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn85fqrnn0jfm1a05di2e.jpg" alt="Angular ContentAfterCheck" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Angular framework offers various lifecycle methods during the process of loading and rendering components. One of these is the ContentAfterCheck lifecycle hook.&lt;/p&gt;

&lt;p&gt;The ContentAfterCheck lifecycle hook is called &lt;strong&gt;after the content of a component has been loaded&lt;/strong&gt; and its first render process has completed. Additionally, it can be called again during the process of updating the component’s content.&lt;/p&gt;

&lt;p&gt;This lifecycle hook is perfect for executing logic that should take place once the content of the component has been loaded and rendered. Examples of such logic include measuring the size of a DOM element within the component or updating the UI based on the component’s content.&lt;/p&gt;

&lt;p&gt;The ContentAfterCheck lifecycle hook can be defined in the &lt;strong&gt;ComponentMetadata&lt;/strong&gt; class of a component. You can implement it by overriding the &lt;strong&gt;ngAfterContentChecked() method&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;AfterContentChecked&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./test.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./test.component.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;AfterContentChecked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;ngAfterContentChecked&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// logic to be executed when the component's content is updated&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>blockchain</category>
      <category>cryptocurrency</category>
      <category>web3</category>
      <category>crypto</category>
    </item>
    <item>
      <title>Implement Caching in Angular with HttpInterceptor</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Sun, 29 Jan 2023 09:55:04 +0000</pubDate>
      <link>https://dev.to/cagkanmert/implement-caching-in-angular-with-httpinterceptor-1ao5</link>
      <guid>https://dev.to/cagkanmert/implement-caching-in-angular-with-httpinterceptor-1ao5</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F48my5k0el0yonpeiq3id.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F48my5k0el0yonpeiq3id.jpg" alt="Caching with HttpInterceptor" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learn how to implement &lt;strong&gt;caching with HttpInterceptor&lt;/strong&gt; in Angular for improved performance and speed. Angular offers the HttpInterceptor class to intercept HTTP requests and cache responses. Follow these simple steps to implement caching with Angular HttpInterceptor:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Take a quick look:&lt;/strong&gt; &lt;a href="https://stackblitz.com/edit/caching-with-http-interceptor?file=src%2Fapp%2Fapp.component.html" rel="noopener noreferrer"&gt;Edit on StackBlitz ⚡️&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Create a new Angular interceptor.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng generate interceptor cache&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; Check if the request is cacheable by verifying it’s a GET request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpRequest&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HttpEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// your code here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; Response should be already in cache using the &lt;strong&gt;Map object&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpRequest&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HttpEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cachedResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedResponse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// your code here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4)&lt;/strong&gt; If the response is not in cache, send the request to the server and store the response in cache using the &lt;strong&gt;tap operator&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpRequest&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HttpEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cachedResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedResponse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using caching with HttpInterceptor in Angular, you can reduce server calls and enhance the performance of your application. Optimize your Angular app now with this helpful caching technique.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;a href="https://cagkanmert.com" rel="noopener noreferrer"&gt;Check out my website for more...&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>marketing</category>
    </item>
    <item>
      <title>Most Common Five TypeScript Errors</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Wed, 25 Jan 2023 18:49:23 +0000</pubDate>
      <link>https://dev.to/cagkanmert/most-common-five-typescript-errors-5ce0</link>
      <guid>https://dev.to/cagkanmert/most-common-five-typescript-errors-5ce0</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Feftndlamv7x7s3zbc6zx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Feftndlamv7x7s3zbc6zx.png" alt="Common TypeScript Errors" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeScript is a powerful open-source programming language that is a superset of JavaScript. It adds optional static typing, class-based object-oriented programming, and other features to JavaScript, making it a reliable language for large-scale software development. However, like any programming language, TypeScript is not without its errors. In this article, we will discuss some of the most common TypeScript errors and how to fix them to help developers write reliable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  “Cannot find module” error
&lt;/h2&gt;

&lt;p&gt;This occurs when TypeScript is &lt;strong&gt;unable to find a module that has been imported in the code&lt;/strong&gt;. This can happen when the module is not installed, or the path to the module is incorrect. To fix this error, ensure that the module is installed and that the path to the module is correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  “Cannot find name” error
&lt;/h2&gt;

&lt;p&gt;This occurs when TypeScript is &lt;strong&gt;unable to find a variable or function that has been defined in the code&lt;/strong&gt;. This can happen when the variable or function is not properly imported or when there is a typo in the variable or function name. To fix this error, ensure that the variable or function is properly imported and that the name is spelled correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  “Type ‘x’ is not assignable to type ‘y'” error
&lt;/h2&gt;

&lt;p&gt;This error, occurs when TypeScript detects that &lt;strong&gt;a variable is being assigned a value that is not of the correct type&lt;/strong&gt;. This can happen when a variable is defined with a specific type but is later assigned a value that is of a different type. To fix this error, ensure that the variable is being assigned a value of the correct type.&lt;/p&gt;

&lt;h2&gt;
  
  
  “Property ‘x’ does not exist on type ‘y'” error
&lt;/h2&gt;

&lt;p&gt;This error occurs when TypeScript detects that &lt;strong&gt;a property is being accessed on an object that does not have that property&lt;/strong&gt;. This can happen when an object is being accessed with the wrong type, or when the property is not defined on the object. To fix this error, ensure that the object is of the correct type and that the property is defined on that object.&lt;/p&gt;

&lt;h2&gt;
  
  
  “Argument of type ‘x’ is not assignable to parameter of type ‘y'” error
&lt;/h2&gt;

&lt;p&gt;This occurs when TypeScript detects that &lt;strong&gt;a function is being called with an argument that is not of the correct type&lt;/strong&gt;. This can happen when a function is defined with a specific type for its parameters but is later called with arguments of a different type. To fix this error, ensure that the function is being called with arguments of the correct type.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://cagkanmert.com" rel="noopener noreferrer"&gt;Check out my website for more...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>software</category>
    </item>
    <item>
      <title>Angular OnChanges: Building Robust Applications</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Tue, 24 Jan 2023 21:23:12 +0000</pubDate>
      <link>https://dev.to/cagkanmert/angular-onchanges-building-robust-applications-10gj</link>
      <guid>https://dev.to/cagkanmert/angular-onchanges-building-robust-applications-10gj</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fordm2dtaohkyri0e9rqm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fordm2dtaohkyri0e9rqm.jpg" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angular.io" rel="noopener noreferrer"&gt;Angular&lt;/a&gt; is a widely-used JavaScript framework for building web applications. One of the key features of Angular is its use of &lt;strong&gt;lifecycle hooks&lt;/strong&gt;, which allow developers to tap into different stages of a component’s lifecycle and perform specific actions at each stage. One of the most useful of these hooks is the &lt;strong&gt;OnChanges lifecycle hook&lt;/strong&gt;, which is called whenever a bound input property of a component changes.&lt;/p&gt;

&lt;p&gt;The OnChanges lifecycle hook is a method that is defined on a component class and decorated with the &lt;em&gt;@OnChanges&lt;/em&gt; decorator. This method takes a &lt;em&gt;single parameter&lt;/em&gt;, an object that contains information about the changes that have occurred to the input properties of the component. The object is of type &lt;strong&gt;SimpleChanges&lt;/strong&gt;, which has a property for each input property of the component, and each property contains the current and previous values of the input property.&lt;/p&gt;

&lt;p&gt;The OnChanges lifecycle hook is &lt;strong&gt;called after&lt;/strong&gt; the first &lt;em&gt;ngOnInit&lt;/em&gt; lifecycle hook &lt;strong&gt;and before&lt;/strong&gt; the &lt;em&gt;ngDoCheck&lt;/em&gt; and &lt;em&gt;ngAfterContentChecked&lt;/em&gt; hooks. It’s also called whenever an input property of the component changes. This makes it a useful tool for performing actions in response to changes to input properties, such as updating the component’s internal state or triggering an API call.&lt;/p&gt;

&lt;p&gt;An example of a simple component that uses the OnChanges lifecycle hook is:&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, OnChanges, Input } from '@angular/core';

@Component({
  selector: 'app-test',
  template: `
    &amp;lt;p&amp;gt;{{message}}&amp;lt;/p&amp;gt;
  `
})
export class TestComponent implements OnChanges {
  @Input() name: string;
  message: string;

  ngOnChanges(changes: SimpleChanges) {
    if (changes.name) {
      this.message = `Hello, ${changes.name.currentValue}!`;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the component has an input property called “name” that is bound to a value in the parent component. The ngOnChanges method is called whenever the value of the “name” input property changes. Inside the ngOnChanges method, the changes object is used to check if the “name” property has changed, and if so, the component’s “message” property is updated to display a greeting with the current value of the “name” property.&lt;/p&gt;

&lt;p&gt;It’s important to note that if you want to update the value of an input property inside the ngOnChanges method, it’s recommended to use the ngDoCheck lifecycle hook, since ngOnChanges only tracks the first change of input properties and not subsequent changes.&lt;/p&gt;

&lt;p&gt;In conclusion, the OnChanges lifecycle hook is an essential tool for responding to changes in input properties in Angular components. It enables developers to perform specific actions in response to changes and keep the component’s internal state in sync with the bound input properties, which is crucial for building robust and maintainable Angular applications. With the Angular OnChanges lifecycle hook, developers can ensure that their Angular applications are responsive, efficient and adaptable to changes in input properties.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://cagkanmert.com" rel="noopener noreferrer"&gt;Check out my website for more...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>discuss</category>
      <category>workplace</category>
    </item>
    <item>
      <title>Environment files are back with Angular v15.1</title>
      <dc:creator>Cagkan Mert Oztas</dc:creator>
      <pubDate>Mon, 23 Jan 2023 07:09:14 +0000</pubDate>
      <link>https://dev.to/cagkanmert/environment-files-are-back-with-angular-v151-5b23</link>
      <guid>https://dev.to/cagkanmert/environment-files-are-back-with-angular-v151-5b23</guid>
      <description>&lt;p&gt;As we all know, when creating a new project in Angular v15, environment files were no longer included. To bring back the environment files, you had to create the files manually, edit the angular.json, and replace the files when switching to production mode.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Angular v15.1 update&lt;/strong&gt; comes with a great command for this. Now, you can automatically create your environment files and set the fileReplacement settings with the&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng generate environment [--project-name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;command.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://cagkanmert.com" rel="noopener noreferrer"&gt;Check out my website for more…&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
