<?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: Revanth Oleti</title>
    <description>The latest articles on DEV Community by Revanth Oleti (@revanth_oleti).</description>
    <link>https://dev.to/revanth_oleti</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%2F693363%2Fa51eec0b-9fab-409c-ab3c-f9f2464ff6c8.png</url>
      <title>DEV Community: Revanth Oleti</title>
      <link>https://dev.to/revanth_oleti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/revanth_oleti"/>
    <language>en</language>
    <item>
      <title>Angular Core Elements[ng]</title>
      <dc:creator>Revanth Oleti</dc:creator>
      <pubDate>Tue, 23 Nov 2021 10:57:49 +0000</pubDate>
      <link>https://dev.to/revanth_oleti/angular-core-templates-jaf</link>
      <guid>https://dev.to/revanth_oleti/angular-core-templates-jaf</guid>
      <description>&lt;p&gt;There are several things under the hood of Angular core library. Here, we are going to look three templates which are mostly confused and also used by every angular developer&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;ng-container&lt;/li&gt;
&lt;li&gt;ng-content&lt;/li&gt;
&lt;li&gt;ng-template&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  ng-container
&lt;/h2&gt;

&lt;p&gt;It only holds the directives without creating the HTML template. We can apply conditional expressions to this tag and insert the template in it, so that the template in it will be loaded with respect to the condition provided, this may be helpful when there is a need to apply multiple conditions on single element.&lt;/p&gt;

&lt;p&gt;Below is the basic example how we can apply conditional expressions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---------what you right-------------------
&amp;lt;ng-container *ngIf="isIronMan"&amp;gt;
  &amp;lt;h1&amp;gt;Tony Stark&amp;lt;/h1&amp;gt;
  &amp;lt;p&amp;gt;.......&amp;lt;/p&amp;gt;
&amp;lt;/ng-container&amp;gt;

--------what it loads in DOM---------------
&amp;lt;h1&amp;gt;Tony Stark&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;.......&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ng-template
&lt;/h2&gt;

&lt;p&gt;The template in it doesn't load until unless we attach it to the container. We can apply as many conditions as we want if we attach it to a directive. These are instances of &lt;code&gt;TemplateRef&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;We need to pass the reference of the template to &lt;code&gt;ViewContainerRef&lt;/code&gt; class &lt;code&gt;createEmbededView&lt;/code&gt; method, which in turns load it in the container. Developers have full control on how and when it should display.&lt;/p&gt;

&lt;p&gt;We can also apply structural directives on the template. You can understand it in the below example.&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;ng-template [ngIf]="isAdmin" [ngIfElse]="loading"&amp;gt;
  &amp;lt;h1&amp;gt;Admin&amp;lt;/h1&amp;gt;
&amp;lt;/ng-template&amp;gt;
&amp;lt;ng-template #loading&amp;gt;
  &amp;lt;h1&amp;gt;Loading....&amp;lt;/h1&amp;gt;
&amp;lt;ng-template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we can see how conditions changes the template rendering. &lt;code&gt;ngIf&lt;/code&gt; checks whether &lt;code&gt;isAdmin&lt;/code&gt; is true or false. If true it loads the first template or else it loads the template referred with loading name.&lt;/p&gt;

&lt;h2&gt;
  
  
  ng-content
&lt;/h2&gt;

&lt;p&gt;In simple terms we can say it as replacement for Iframes. We create directives and the content inside that particular directive selector tag will be loaded into the directive and We can specify where to project the content inside the template by assigning to an attribute called select.&lt;br&gt;
It doesn't create any DOM element on its name.&lt;/p&gt;

&lt;p&gt;We can understand more with the below example.&lt;br&gt;
General way of defining ng-content with directive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;----------------Directive--------------
import { Component } from '@angular/core';
@Component({
  selector: 'app-test',
  template: `
    &amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
    &amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
  `
})
export class TestComponent {}

----------------Another Component--------
&amp;lt;app-test&amp;gt;
  &amp;lt;p&amp;gt;From another component&amp;lt;/p&amp;gt;
&amp;lt;/app-test&amp;gt;

-----------------Output--------------
&amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;From another component&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;If the attribute matches, then content is loaded into the specified position.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
@Component({
  selector: 'app-test',
  template: `
    &amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
    &amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
    &amp;lt;ng-content select="[testProjection]"&amp;gt;&amp;lt;/ng-content&amp;gt;
  `
})
export class TestComponent {}

----------------Another Component--------
&amp;lt;app-test&amp;gt;
  &amp;lt;p testProjection&amp;gt;From another component&amp;lt;/p&amp;gt;
&amp;lt;/app-test&amp;gt;

-----------------Output--------------
&amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;From another component&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;If the attribute doesn't match then it loads into the default position.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
@Component({
  selector: 'app-test',
  template: `
    &amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
    &amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
    &amp;lt;ng-content select="[testProjection]"&amp;gt;&amp;lt;/ng-content&amp;gt;
  `
})
export class TestComponent {}

----------------Another Component--------
&amp;lt;app-test&amp;gt;
  &amp;lt;p testProjection&amp;gt;From Example 1 another component&amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;From Example 2 another component&amp;lt;/p&amp;gt;
&amp;lt;/app-test&amp;gt;

-----------------Output--------------
&amp;lt;h1&amp;gt;Test content&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;From Example 2 another component&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;From Example 1 another component&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second case even the &lt;strong&gt;p&lt;/strong&gt; tag with testProjection is written first, due to ng-content select attribute with testProjection is loaded after default ng-content it will be loaded after the second &lt;strong&gt;p&lt;/strong&gt; tag.&lt;/p&gt;

&lt;p&gt;Hope you got the clarity and know the differences between all the three elements.&lt;/p&gt;

&lt;p&gt;Happy coding.....&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Difference between Subject and BehaviorSubject </title>
      <dc:creator>Revanth Oleti</dc:creator>
      <pubDate>Thu, 28 Oct 2021 11:25:25 +0000</pubDate>
      <link>https://dev.to/revanth_oleti/difference-between-subject-and-behaviorsubject-9g6</link>
      <guid>https://dev.to/revanth_oleti/difference-between-subject-and-behaviorsubject-9g6</guid>
      <description>&lt;p&gt;Before stepping into the differences, first let us know what Subject and BehaviorSubject actually are? With RxJS coming into the picture, Angular became much more asynchronous. Let us discuss with asynchronous is.&lt;/p&gt;

&lt;p&gt;To display users data when ever the user logged in, We need to get the users data from internet or server or some where else which is not available in the application. To do that we are making some HttpRequest to the server asking for it. Thus we don't know how long it will take to get the data. The response didn't come synchronously or line after line of code, instead it will be available when ever it comes. It is called asynchronous. &lt;/p&gt;

&lt;p&gt;To get the asynchronous data, there is a tool called Observable which is provided by RxJS library. It is a collection of objects overtime that we use to contain data retrieved from asynchronous calls. It works on subscription basis, same as Netflix and Amazon. If we subscribe to an observable, then we can get the data that the observable have and also it will be updated when new data is available. If we unsubscribe it, then we can't see or get the data and also can't update the existing data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const source = new BehaviorSubject&amp;lt;string&amp;gt;('');
source.next('hi')
source.next('hello')
source.subscribe(x=&amp;gt;
  console.log(x)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output will be:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;For every new value updated in the source, it will be display in the output console until the source was unsubscribed.&lt;/p&gt;

&lt;p&gt;Both Subject and BehaviorSubject are Observables. Let us know more about each observable with simple example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Subject
&lt;/h2&gt;

&lt;p&gt;From the below example you can see, we have a Subject called mySubject and it has three subscribers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySubject = new Subject&amp;lt;number&amp;gt;();
mySubject.subscribe({
  next: (z) =&amp;gt; console.log('a');
});
mySubject.subscribe({
  next: (z) =&amp;gt; console.log('b');
});
mySubject.next(1)
mySubject.next(2)

mySubject.subscribe({
  next: (z) =&amp;gt; console.log('c');
});

mySubject.next(3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;First we are assigning value &lt;code&gt;1&lt;/code&gt; to mySubject using &lt;code&gt;mySubject.next(1)&lt;/code&gt;, Where it will call both subscribers and in the console it will print &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Second we are assigning value &lt;code&gt;2&lt;/code&gt; to mySubject using &lt;code&gt;mySubject.next(2)&lt;/code&gt;, Where it will call both subscribers and in the console it will print &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then we created another subscriber to mySubject. Later by assigning &lt;code&gt;3&lt;/code&gt; to mySubject using &lt;code&gt;mySubject.next(3)&lt;/code&gt;, It will call all three subscribers and in console it will print &lt;code&gt;a&lt;/code&gt;,&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Output in the console will be
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a
b
a
b
a
b
c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you know how the subject works, let us also know how BehaviorSubject works.&lt;/p&gt;

&lt;h2&gt;
  
  
  BehaviorSubject
&lt;/h2&gt;

&lt;p&gt;It also has same syntax as Subject. But with the BehaviorSubject you can get the current value on Subscription. Whenever if you subscribe using the observable, it will immediately retrieves the current value. Lets understand with a small example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var behariorSubject = new BehaviorSubject&amp;lt;number&amp;gt;(0);
behariorSubject.subscribe({
  next: (z) =&amp;gt; console.log('observerA: ' + z) 
});

behariorSubject.next(1);  

behariorSubject.next(2);   

behariorSubject.subscribe({
  next: (z) =&amp;gt; console.log('observerB: ' + z)  
});

behariorSubject.next(3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we have a behaviourSubject with two subscribers and assigning three values. Irrespective of when it is subscribed, all the subscribers get the latest values of the behaviorSubject. &lt;br&gt;
Output in the console will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;observerA: 0
observerA: 1
observerA: 2
observerB: 2
observerA: 3
observerB: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Subject Vs BehaviorSubject
&lt;/h2&gt;

&lt;p&gt;Now you know how both Subject and BehaviorSubject works. The main problem you get is that when can we use Subject and when can we use BehaviorSubject. By knowing the difference between them you can get idea when to use which observable. &lt;/p&gt;

&lt;h3&gt;
  
  
  Values they hold
&lt;/h3&gt;

&lt;p&gt;If you subscribe to a Subject, we wont get current value or initial value. &lt;br&gt;
But when you subscirbe to a BehaviorSubject, you will get the current or initial value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initial Value
&lt;/h3&gt;

&lt;p&gt;For Subject, we don't need to initial a value.&lt;br&gt;
But whenever we declare BehaviorSubject we need to initialize a default value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subscribers
&lt;/h3&gt;

&lt;p&gt;In Subject, the subscribers will only receive the upcoming value.&lt;br&gt;
In BehaviorSubject, the subscribers will receive the previous value and also upcoming value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;These are some of the key differences between Subject and BehaviorSubject. You can learn more about observables if you visit this &lt;a href="https://rxjs.dev/guide/observable"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Coding...&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
