<?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: Tejas</title>
    <description>The latest articles on DEV Community by Tejas (@jastya16).</description>
    <link>https://dev.to/jastya16</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%2F695413%2Ff92cd779-c601-444c-a834-09f7670adce9.png</url>
      <title>DEV Community: Tejas</title>
      <link>https://dev.to/jastya16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jastya16"/>
    <language>en</language>
    <item>
      <title>Angular: ngAfterContentChecked</title>
      <dc:creator>Tejas</dc:creator>
      <pubDate>Wed, 04 Jun 2025 17:08:34 +0000</pubDate>
      <link>https://dev.to/jastya16/angular-ngaftercontentchecked-5kp</link>
      <guid>https://dev.to/jastya16/angular-ngaftercontentchecked-5kp</guid>
      <description>&lt;h2&gt;
  
  
  Angular lifecycle hook - &lt;code&gt;ngAfterContentChecked&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In angular we can project content from parent component to child component using &lt;code&gt;&amp;lt;ng-content&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whenever the projected content is initialized, then the &lt;code&gt;ngAfterContentInit&lt;/code&gt; lifecycle hook gets executed in the child component.&lt;br&gt;
And whenever there's any change in the projected content, the &lt;code&gt;ngAfterContentChecked&lt;/code&gt; lifecycle hook gets executed in the child component.&lt;/p&gt;



&lt;p&gt;Let's create a child component that&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;displays the content projected by the parent component using &lt;code&gt;&amp;lt;ng-content&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;implements the &lt;code&gt;AfterCintentInit&lt;/code&gt; and &lt;code&gt;AfterContentChecked&lt;/code&gt; classes and the methods &lt;code&gt;ngAfterCintentInit&lt;/code&gt; and &lt;code&gt;ngAfterCintentInit&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-child',
  standalone: true,
  template: `
    &amp;lt;h3&amp;gt;Child component&amp;lt;/h3&amp;gt;
    &amp;lt;p&amp;gt;Hello from child component&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;Below is the content from parent component&amp;lt;/p&amp;gt;
    &amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
  `,
})
export class ChildComponent implements AfterContentInit, AfterContentChecked {
  ngAfterContentInit() {
    console.log('ngAfterContentInit() is called in ChildComponent');
  }
  ngAfterContentChecked() {
    console.log('ngAfterContentChecked() is called in ChildComponent');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's create a parent component that &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;projects some content to the child component
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'app-parent',
  standalone: true,
  imports: [ChildComponent, CommonModule],
  template: `
    &amp;lt;h3&amp;gt;Parent component&amp;lt;/h3&amp;gt;
    &amp;lt;p&amp;gt;Hello from parent component&amp;lt;/p&amp;gt;
    &amp;lt;app-child&amp;gt;
    &amp;lt;!-- This is the content that we project into the child component
      Parent component has control over it
      --&amp;gt;
      &amp;lt;span&amp;gt;This is the name in parent component which is projected into the child component = &amp;lt;/span&amp;gt;&amp;lt;span&amp;gt;&amp;lt;b&amp;gt;{{counter$ | async}}&amp;lt;/b&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;/app-child&amp;gt;
  `,
})
export class ParentComponent{
  counter$ = new BehaviorSubject&amp;lt;number&amp;gt;(1);
  constructor() {
    /**
     * We're setting 2500 ms timeout
     * After 2500 ms, the callback function is executed which pushes value into the BehaviorSubject
     * Since we're projecting this content (with updated name value) into the child, the ngAfterContentChecked() lifecycle hook will get executed in the child component
     */
    setInterval(() =&amp;gt; this.counter$.next(+this.counter$.value + 1), 2500);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's use this parent component into the root component of our angular app so that we can serve the app and see results&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'zone.js';
import { Component, AfterContentInit, AfterContentChecked } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { BehaviorSubject } from 'rxjs';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [ParentComponent],
  template: `
    &amp;lt;h1&amp;gt;Hello from {{ name }}!&amp;lt;/h1&amp;gt;
    &amp;lt;app-parent&amp;gt;&amp;lt;/app-parent&amp;gt;
  `,
})
export class App {}

bootstrapApplication(App);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ngAfterContentChecked()&lt;/code&gt; method in the child component gets called  each &lt;code&gt;2500&lt;/code&gt;ms because the content projected from the parent component is changing every &lt;code&gt;2500&lt;/code&gt;ms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here is the working example - &lt;a href="https://stackblitz.com/edit/angular-fyaucn66?file=src%2Fmain.ts" rel="noopener noreferrer"&gt;https://stackblitz.com/edit/angular-fyaucn66?file=src%2Fmain.ts&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;u&gt;Note&lt;/u&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the console on the bottom right in the stackblitz to see the console logs&lt;/li&gt;
&lt;li&gt;Sometimes the console output in the Stachblitz is random, do hit the refresh icon of the preview and it'll work fine.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>angular</category>
      <category>ngaftercontentchecked</category>
      <category>lifecyclehook</category>
      <category>ngaftercontentinit</category>
    </item>
    <item>
      <title>RxJs: withLatestFrom</title>
      <dc:creator>Tejas</dc:creator>
      <pubDate>Fri, 10 Sep 2021 16:22:37 +0000</pubDate>
      <link>https://dev.to/jastya16/rxjs-withlatestfrom-1110</link>
      <guid>https://dev.to/jastya16/rxjs-withlatestfrom-1110</guid>
      <description>&lt;p&gt;Hi all, in this post we will learn how to use RxJs &lt;code&gt;withLatestFrom&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;Let's say we have 2 observables bs1, bs2. We are interested in getting notified whenever bs1 streams any data. Also, we want to consider latest value streamed by bs2 whenever bs1 streams data. In this case we can make use of &lt;code&gt;withLatestFrom&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We subscribe to bs1 &amp;amp; use &lt;code&gt;withLatestFrom(bs2)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use this stackBlitz link to get an example: &lt;a href="https://stackblitz.com/edit/rxjs-uhrmor?devtoolsheight=60" rel="noopener noreferrer"&gt;https://stackblitz.com/edit/rxjs-uhrmor?devtoolsheight=60&lt;/a&gt;&lt;br&gt;
You need to observe the console logs to understand the use of &lt;code&gt;withLatestFrom&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I hope this helps.&lt;/p&gt;

&lt;p&gt;Thanks!!&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>withlatestfrom</category>
      <category>rxjsoperators</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
