<?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: Deekshith Raj Basa 🔥</title>
    <description>The latest articles on DEV Community by Deekshith Raj Basa 🔥 (@deekshithrajbasa).</description>
    <link>https://dev.to/deekshithrajbasa</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%2F473201%2Fdaccfccf-e0d9-4d5f-b6f1-81220e364db6.jpg</url>
      <title>DEV Community: Deekshith Raj Basa 🔥</title>
      <link>https://dev.to/deekshithrajbasa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deekshithrajbasa"/>
    <language>en</language>
    <item>
      <title>Understanding Transformer Architecture for Large Language Models (LLMs)</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Thu, 05 Jun 2025 03:39:05 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/understanding-transformer-architecture-for-large-language-models-llms-i12</link>
      <guid>https://dev.to/deekshithrajbasa/understanding-transformer-architecture-for-large-language-models-llms-i12</guid>
      <description>&lt;p&gt;If you've heard of ChatGPT, Gemini, or Llama, you know they're powered by &lt;strong&gt;Large Language Models (LLMs)&lt;/strong&gt;. But what makes these models so powerful? The secret lies in their core structure—the &lt;strong&gt;Transformer architecture&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;In this post, we’ll break down the Transformer architecture in simple terms so you can understand how it works.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is a Transformer?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Introduced in the 2017 paper &lt;a href="https://arxiv.org/abs/1706.03762" rel="noopener noreferrer"&gt;"Attention Is All You Need"&lt;/a&gt; by Vaswani et al., the &lt;strong&gt;Transformer&lt;/strong&gt; is a deep learning model designed to handle sequential data (like text) more efficiently than older models (RNNs, LSTMs).  &lt;/p&gt;

&lt;p&gt;Unlike older models that process words one by one, Transformers can look at &lt;strong&gt;all words in a sentence at once&lt;/strong&gt; using a mechanism called &lt;strong&gt;self-attention&lt;/strong&gt;. This makes them faster and better at understanding context.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Key Components of a Transformer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A Transformer has two main parts:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Encoder&lt;/strong&gt; (processes input text)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decoder&lt;/strong&gt; (generates output text)
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For LLMs like GPT, only the &lt;strong&gt;decoder&lt;/strong&gt; is used (since they focus on generating text). Models like BERT use only the &lt;strong&gt;encoder&lt;/strong&gt; (since they focus on understanding text).  &lt;/p&gt;

&lt;p&gt;Let’s break down the key components:  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Tokenization &amp;amp; Embeddings&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Words are split into smaller pieces called &lt;strong&gt;tokens&lt;/strong&gt; (e.g., "learning" → "learn" + "ing").
&lt;/li&gt;
&lt;li&gt;Each token is converted into a &lt;strong&gt;vector (list of numbers)&lt;/strong&gt; using &lt;strong&gt;word embeddings&lt;/strong&gt; (e.g., Word2Vec, GloVe).
&lt;/li&gt;
&lt;li&gt;Since word order matters, &lt;strong&gt;positional encodings&lt;/strong&gt; are added to give the model information about word positions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Self-Attention Mechanism&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;most important part&lt;/strong&gt; of a Transformer.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Self-attention&lt;/strong&gt; allows the model to weigh the importance of each word relative to others.
&lt;/li&gt;
&lt;li&gt;Example:

&lt;ul&gt;
&lt;li&gt;In the sentence &lt;em&gt;"The cat sat on the mat because it was tired."&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The word &lt;em&gt;"it"&lt;/em&gt; refers to &lt;em&gt;"cat"&lt;/em&gt;. Self-attention helps the model link these words.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The model computes &lt;strong&gt;attention scores&lt;/strong&gt; to decide how much focus each word should get.
&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Multi-Head Attention&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Instead of one attention mechanism, Transformers use &lt;strong&gt;multiple attention heads&lt;/strong&gt; in parallel.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each head looks at different relationships between words (e.g., one head may focus on subject-verb, another on pronouns).
&lt;/li&gt;
&lt;li&gt;This makes the model more powerful in understanding context.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Feed-Forward Neural Networks (FFNN)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After attention, each token passes through a simple neural network to process the information further.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;5. Layer Normalization &amp;amp; Residual Connections&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Residual connections&lt;/strong&gt; help avoid the "vanishing gradient" problem (allowing deep networks to train better).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layer normalization&lt;/strong&gt; stabilizes training by normalizing values between layers.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6. Decoder (for Text Generation)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The decoder works similarly but has an extra &lt;strong&gt;masked self-attention&lt;/strong&gt; layer to prevent it from "cheating" by looking at future words.
&lt;/li&gt;
&lt;li&gt;It generates text &lt;strong&gt;one word at a time&lt;/strong&gt;, using previous outputs as inputs (autoregressive generation).
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Transformers Are Better Than RNNs/LSTMs?&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;RNNs/LSTMs&lt;/th&gt;
&lt;th&gt;Transformers&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Parallel Processing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ (Sequential)&lt;/td&gt;
&lt;td&gt;✅ (All words at once)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Long-range Dependencies&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ (Struggles with long sentences)&lt;/td&gt;
&lt;td&gt;✅ (Self-attention captures distant relationships)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Training Speed&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ (Slow due to sequential steps)&lt;/td&gt;
&lt;td&gt;✅ (Faster due to parallelization)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;How Transformers Power LLMs?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Models like &lt;strong&gt;GPT-4, Llama 2, Gemini&lt;/strong&gt; are &lt;strong&gt;decoder-only Transformers&lt;/strong&gt; trained on massive text data. They:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Take a &lt;strong&gt;prompt&lt;/strong&gt; (input text).
&lt;/li&gt;
&lt;li&gt;Process it through multiple Transformer layers.
&lt;/li&gt;
&lt;li&gt;Predict the &lt;strong&gt;next word&lt;/strong&gt; repeatedly to generate responses.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Transformers use &lt;strong&gt;self-attention&lt;/strong&gt; to understand relationships between words.
&lt;/li&gt;
&lt;li&gt;They process &lt;strong&gt;all words at once&lt;/strong&gt; (unlike RNNs).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-head attention&lt;/strong&gt; helps capture different word relationships.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Positional embeddings&lt;/strong&gt; help track word order.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decoder-only models&lt;/strong&gt; (like GPT) generate text autoregressively.
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>gpt3</category>
      <category>genai</category>
      <category>chatgpt</category>
    </item>
    <item>
      <title>Improving Angular App Performance</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Mon, 11 Mar 2024 18:18:48 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/improving-angular-app-performance-4gp0</link>
      <guid>https://dev.to/deekshithrajbasa/improving-angular-app-performance-4gp0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Improving Angular App Performance: Profiling and Code Optimization Strategies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A blazing-fast Angular application is the backbone of a great user experience. But as features grow, performance bottlenecks can creep in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Profiling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Embrace the Angular Profiler:&lt;/strong&gt; This built-in tool helps pinpoint sluggish components and exposes the change detection cycle for scrutiny.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome DevTools to the Rescue:&lt;/strong&gt; Dive deeper with Chrome DevTools. Analyze network requests, JavaScript execution times, and identify performance drains.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angular.io/guide/devtools#profile-your-application" rel="noopener noreferrer"&gt;https://angular.io/guide/devtools#profile-your-application&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Change Detection Optimization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OnPush for Efficiency:&lt;/strong&gt; Implement the OnPush change detection strategy in components with minimal updates. This minimizes unnecessary checks, boosting performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;trackBy for ngFor Finesse:&lt;/strong&gt; When using ngFor for lists, leverage the trackBy function. This optimizes how Angular tracks list items, preventing needless re-renders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angular.io/guide/change-detection" rel="noopener noreferrer"&gt;https://angular.io/guide/change-detection&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Bundle Size Reduction:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AOT Compilation for the Win:&lt;/strong&gt; Enable Ahead-of-Time (AOT) compilation. This generates smaller, optimized JavaScript bundles, leading to faster loading times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tree-Shaking Magic:&lt;/strong&gt; Utilize tree-shaking, a process that intelligently removes unused code from your final build. This keeps your app lean and mean.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/front-end-weekly/optimize-angular-bundle-size-f5392f0ae947" rel="noopener noreferrer"&gt;https://medium.com/front-end-weekly/optimize-angular-bundle-size-f5392f0ae947&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://angularindepth.com/posts/1217/how-to-reuse-common-layouts-in-angular-using-router" rel="noopener noreferrer"&gt;https://angularindepth.com/posts/1217/how-to-reuse-common-layouts-in-angular-using-router&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  angular #performance #optimization #webdev #softwareengineering #js
&lt;/h1&gt;

</description>
      <category>angular</category>
      <category>performance</category>
      <category>optimization</category>
      <category>treeshaking</category>
    </item>
    <item>
      <title>Implementing Native Drag-and-Drop in Angular</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Tue, 05 Mar 2024 04:19:58 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/implementing-native-drag-and-drop-in-angular-3epm</link>
      <guid>https://dev.to/deekshithrajbasa/implementing-native-drag-and-drop-in-angular-3epm</guid>
      <description>&lt;p&gt;Drag-and-drop functionality can significantly enhance the user experience in your Angular applications, allowing for intuitive interactions like reordering items, building lists, and managing file uploads.&lt;/p&gt;

&lt;p&gt;Leverage the browser's built-in Drag-and-Drop API for a straightforward implementation. Utilize the dragstart, dragover, and drop events to capture drag interactions and update your application state accordingly.&lt;/p&gt;

&lt;h4&gt;
  
  
  HTML
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="draggable" (dragover)="onDragOver($event)" (drop)="onDrop($event)"&amp;gt; Drag and Drop
    &amp;lt;/div&amp;gt;
&amp;lt;ng-container *ngIf="droppedImage"&amp;gt;
  &amp;lt;img  [src]="droppedImage" alt="Dropped Image"/&amp;gt;
&amp;lt;/ng-container&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Typescript
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;droppedImage: string | null = null;

  onDragOver(event: DragEvent) {
    event.preventDefault(); // Allow dropping
  }

  onDrop(event: DragEvent) {
    if (event.dataTransfer) {
      const files = event.dataTransfer.files;
      if (files &amp;amp;&amp;amp; files.length === 1 &amp;amp;&amp;amp; files[0].type.startsWith('image/')) {
        const reader = new FileReader();
        reader.readAsDataURL(files[0]);
        reader.onload = (e) =&amp;gt; {
          this.droppedImage = e?.target?.result as string;
        };
      } else {
        alert('Please drop an image file only.');
      }
    }
    event.preventDefault(); // Not letting the browser to open image in new tab
  }

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

&lt;/div&gt;



&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%2Flbl2uoua4evlygb09j14.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%2Flbl2uoua4evlygb09j14.png" alt="Native Drag-and-Drop in Angular" width="800" height="815"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the full working demo: &lt;a href="https://stackblitz.com/edit/native-drag-drop-angular-deekshith-raj-basa?file=src%2Fmain.ts" rel="noopener noreferrer"&gt;https://stackblitz.com/edit/native-drag-drop-angular-deekshith-raj-basa?file=src%2Fmain.ts&lt;/a&gt;&lt;/p&gt;

</description>
      <category>draganddrop</category>
      <category>fileupload</category>
      <category>angular</category>
      <category>nativedragdrop</category>
    </item>
    <item>
      <title>Build Custom Structural Directives in Angular like a hero 😎</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Mon, 01 Aug 2022 05:05:10 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/build-custom-structural-directives-in-angular-like-a-hero-2nk6</link>
      <guid>https://dev.to/deekshithrajbasa/build-custom-structural-directives-in-angular-like-a-hero-2nk6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Angular comes with some built-in directives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structural Directive&lt;/strong&gt; can be used to manipulate the HTML structure in the DOM. Using them, we can change the structure of part of the DOM.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;*ngIf&lt;/li&gt;
&lt;li&gt;*ngForOf&lt;/li&gt;
&lt;li&gt;*ngSwitch&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating a custom structural directive
&lt;/h2&gt;

&lt;p&gt;Why custom structural directive?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We have build in structural directive that satisfies our solution, what if we might come across a case that the ones provided with the framework don't solve. so here comes the custom structural directive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, in this article we will try cloning the &lt;code&gt;*ngIf&lt;/code&gt; structural directive&lt;/p&gt;

&lt;p&gt;Let's understand how we can create &lt;code&gt;*ngIf&lt;/code&gt; structural directive&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's create a project using Angular CLI
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// use this command to create new angular project
ng new project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create a module 
ng generate module custom-if
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create a custom directive&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create a custom directive
ng generate directive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;generated directive should look like this&lt;br&gt;
&lt;/p&gt;

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

@Directive({
  selector: '[customIf]',
})
export class customIfDirective {
  constructor() {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's implement the basic functionality of displaying the content if passed value is true&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;h2 *customIf="true"&amp;gt;My visible content&amp;lt;/h2&amp;gt;
&amp;lt;h2 *customIf="false"&amp;gt;My hidden content&amp;lt;/h2&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To achieve that, we need a couple of elements:&lt;/p&gt;

&lt;p&gt;an input that will determine whether to show or hide the content &lt;code&gt;(@Input)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;a reference to the template that we want to conditionally display &lt;code&gt;(TemplateRef)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;a container that will provide us with access to Angular's view &lt;code&gt;(ViewContainerRef)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@input&lt;/code&gt; can be just a regular class field decorators property with Angular's. For it to work as it does in the example code shown&lt;code&gt;*customIf="true"&lt;/code&gt;, we need to name the property the same as the attribute's selector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Directive, Input } from '@angular/core';

@Directive({
  selector: '[customIf]',
})
export class IfDirective {
 @Input() set customIf(show: boolean) {
    //code goes here
  }

  constructor() {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the directive has the value to display the content, we also need TemplateRef and ViewContainerRef instances. We can do that by injecting them by importing from &lt;code&gt;@angular/core&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { TemplateRef, ViewContainerRef } from '@angular/core';

constructor(
    private templateRef: TemplateRef&amp;lt;unknown&amp;gt;,
    private vcr: ViewContainerRef
  ) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can make a use of &lt;code&gt;ViewContainerRef's&lt;/code&gt; reference &lt;code&gt;this.vcr.createEmbeddedView(this.templateRef)&lt;/code&gt; method to display and &lt;code&gt;this.vcr.clear()&lt;/code&gt; method to remove the content.&lt;/p&gt;

&lt;p&gt;This is how the final code looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Directive({
  selector: '[customIf]',
})
export class IfDirective {
@Input() set customIf(value: boolean) {
    this._renderTemplate(value)l
  }

constructor(
    private templateRef: TemplateRef&amp;lt;unknown&amp;gt;,
    private vcr: ViewContainerRef
  ) {}

private _renderTemplate(show: boolean) {
    this.vcr.clear();
    if (show) {
      this.vcr.createEmbeddedView(this.templateRef);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wohoo! 🥳  we have successfully create &lt;code&gt;*customIf&lt;/code&gt; now let's focus on creating else template&lt;br&gt;
So, let us understand how it works?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do we achieve this behaviour in our custom directive? This is where understanding the "syntactic sugar" that stands behind the structural directives is crucial.&lt;/p&gt;
&lt;/blockquote&gt;

&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%2Fq6yorwrdclvmoi3a7rlb.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%2Fq6yorwrdclvmoi3a7rlb.png" alt="Custom Structural Directives" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we observe the above image example, it means that the else property is actually becoming ngIfElse input parameter.&lt;/p&gt;

&lt;p&gt;So, we can access the else template by &lt;code&gt;selector (customIf) + else (Else)&lt;/code&gt; = &lt;code&gt;customIfElse&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Input() customIfElse?: TemplateRef&amp;lt;unknown&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the code looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Directive({
  selector: '[customIf]',
})
export class IfDirective {
@Input() set customIf(value: boolean) {
    this._renderTemplate(value)l
  }
@Input() customIfElse?: TemplateRef&amp;lt;unknown&amp;gt;;
constructor(
    private templateRef: TemplateRef&amp;lt;unknown&amp;gt;,
    private vcr: ViewContainerRef
  ) {}

private _renderTemplate(show: boolean) {
    this.vcr.clear();
    if (show) {
      this.vcr.createEmbeddedView(this.templateRef);
    } else if (this.customIfElse) {
      this.vcr.createEmbeddedView(this.customIfElse);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we've learnt how to create a simple custom structural directive that handles additional inputs. We've covered the syntactic sugar that stands behind the structural directive, and how it translates into directive's inputs.&lt;/p&gt;

&lt;p&gt;In case you have any questions, you can always tweet or DM me at &lt;a href="https://twitter.com/deekshithrajB" rel="noopener noreferrer"&gt;@DeekshithrajB&lt;/a&gt;. I'm always happy to help!&lt;/p&gt;

&lt;p&gt;Connect with me over linkedIn: &lt;a href="https://www.linkedin.com/in/deekshith-raj-basa/" rel="noopener noreferrer"&gt;Deekshith Raj Basa&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>directives</category>
      <category>customstructuraldirective</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular Server-Side Rendering(SSR): The Browser Is Not The Server</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Thu, 12 Nov 2020 13:18:33 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/angular-server-side-rendering-ssr-the-browser-is-not-the-server-30a5</link>
      <guid>https://dev.to/deekshithrajbasa/angular-server-side-rendering-ssr-the-browser-is-not-the-server-30a5</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%2Fi%2F0atmqacjr05tp3z8crir.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%2Fi%2F0atmqacjr05tp3z8crir.png" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the great things about &lt;strong&gt;SSR&lt;/strong&gt; is that we get to use the same code on our &lt;strong&gt;frontend and our backend to render our app&lt;/strong&gt;. Well, sort of.&lt;/p&gt;

&lt;p&gt;When we use the same code, right off the bat we have a problem: &lt;strong&gt;the browser is not the server&lt;/strong&gt; and there are differences between what we can do in each environment.&lt;/p&gt;

&lt;p&gt;The benefit of rendering our Angular app on the server is that we can fetch data &lt;strong&gt;privately and efficiently&lt;/strong&gt; before we send anything to the browser.&lt;/p&gt;

&lt;p&gt;Our server is (in this case) Node.js, and so on the server we can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;request to make HTTP requests retrieved by the server (and these can be private authenticated requests)&lt;/li&gt;
&lt;li&gt;fs to access the filesystem (if we need to)
access to anything else you'd want on the server: Redis, AWS services, the database etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But the browser is not the server. And if we try to call browser-only APIs, then we'll break SSR.&lt;/p&gt;

&lt;h3&gt;
  
  
  What can break SSR?
&lt;/h3&gt;

&lt;p&gt;Well, three things come to mind that is exclusive to the browser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the window can be used to e.g. display alerts to the user&lt;/li&gt;
&lt;li&gt;the document belongs to the window namespace and is used to manipulate DOM elements&lt;/li&gt;
&lt;li&gt;navigator belongs to the window namespace and enables service workers that are used extensively with Progressive Web Applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While it's awesome that our Angular application can share code between the server and the browser, if we want to use any of these objects, we need to execute a different logic path based on the current runtime: Node.js or the browser window.&lt;/p&gt;

&lt;p&gt;Below, I'm going to show you one of the techniques for doing that&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding internationalization
&lt;/h3&gt;

&lt;p&gt;Let's add internationalization to your application. Let's display product prices in three currencies: US dollars, British pounds, and Polish zloty. The application should pick a currency based on browser settings, and if a given language is not supported, it should fall back to Polish zloty&lt;/p&gt;

&lt;p&gt;Let's generate a new service:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g s sample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let's detect user language and implement the getCurrencyCode() method that returns one of the three available currency codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  providedIn: 'root'
})
export class SampleService {

  private userLang;

  constructor() { 
      this.userLang = window.navigator.language;
  }

  public getCurrencyCode(): string {
    switch(this.userLang) {
      default: 
      case 'pl-PL': return 'PLN';
      case 'en-US': return 'USD';
      case 'en-EN': return 'GBP';
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in one of our components, say ProductDetailsComponent, we can use this service to get the user's currency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public userCurrency: string = this.sampleService.getCurrencyCode();

constructor(
  private route: ActivatedRoute, 
  private ps: ProductsService, 
  private us: UserService, 
  private sampleService: SampleService
) { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we could use the userCurrency in a view with the currency pipe:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;pclass="text-muted"&amp;gt;{{userCurrency}}&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From now on, prices should display in a currency defined by the user's localization settings. This is great, right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Well, no. Unfortunately, this logic breaks SSR:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ERROR: ReferenceError: window is not defined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It would help if we had a mechanism to detect whether the current runtime is the browser or the server - and thankfully that's why we have isPlatformBrowser() and isPlatformServer():&lt;/p&gt;

&lt;h3&gt;
  
  
  isPlatformBrowser() and isPlatformServer()
&lt;/h3&gt;

&lt;p&gt;Angular ships with the isPlatformBrowser() and isPlatformServer() methods in the @angular/common package. Each of these methods accepts one parameter: the platform ID. It can be retrieved via the Dependency Injection mechanism using the injection token PLATFORM_ID available in the @angular/core package.&lt;/p&gt;

&lt;p&gt;So to change our internationalization service I18nService above, Add these new imports:&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, 
  Inject, 
  PLATFORM_ID 
  } from '@angular/core';
import { 
  isPlatformBrowser 
  } from '@angular/common';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify the service constructor to only use the window object if an instance of the service executes in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class SampleService {
  constructor(
    @Inject(PLATFORM_ID)
    private platformId: any
  ) {
    if (isPlatformBrowser(this.platformId)) {
      this.userLang =
        window.navigator.language;
    } else {
      // server specific logic
    }
  }
  // ...
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should be enough for SSR to start working again but we don't get internationalization pre-rendered on our server-side render -- internationalization won't appear until after the app loads.&lt;/p&gt;

&lt;p&gt;So what we need is a way to know what language to render from the origin HTTP request to the server.&lt;/p&gt;

&lt;p&gt;The Request object&lt;br&gt;
The question now is how to retrieve information about user language on the server. Is it even possible?&lt;/p&gt;

&lt;p&gt;Yes, it is.&lt;/p&gt;

&lt;p&gt;When you're performing a request from the browser, the browser adds a bunch of HTTP headers that you might not usually think about.&lt;/p&gt;

&lt;p&gt;One of these headers is Accept-Language which tells us what language the user wants!&lt;/p&gt;

&lt;p&gt;For example, the header might come through like this: Accept-Language: en-US, en;q=0.5&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting Headers from the Request
&lt;/h3&gt;

&lt;p&gt;Angular Universal allows you to get an object that represents an HTTP request. It's available via Dependency Injection under the REQUEST token from the @nguniversal/express-engine/tokens package. The Request object contains the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body&lt;/li&gt;
&lt;li&gt;params&lt;/li&gt;
&lt;li&gt;headers&lt;/li&gt;
&lt;li&gt;cookies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we update our imports by adding the Request object, the REQUEST injection token, and the Optional decorator&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, Inject, PLATFORM_ID, Optional } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { Request } from 'express';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the constructor to inject the Request object and retrieve user language from the Accept-Language header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class SampleService {
  constructor(
    @Inject(PLATFORM_ID) private platformId: any,
    @Optional()
    @Inject(REQUEST) private request: Request
  ) {
    if (isPlatformBrowser(this.platformId)) {
      this.userLang =
        window.navigator.language;
    } else {
      this.userLang = (
        this.request.headers[
          "accept-language"
        ] || ""
      ).substring(0, 5);
    }
  }
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>ssr</category>
    </item>
    <item>
      <title>How to Add Rich Text Editor to an Angular App</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Thu, 24 Sep 2020 07:16:28 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/how-to-add-rich-text-editor-to-an-angular-app-4nm2</link>
      <guid>https://dev.to/deekshithrajbasa/how-to-add-rich-text-editor-to-an-angular-app-4nm2</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%2Fi%2Fycnwngzj6es0cwa0ti4l.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%2Fi%2Fycnwngzj6es0cwa0ti4l.jpg" alt="Image" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CKEditor 5 consists of ready-to-use editor builds and CKEditor 5 Framework upon which the builds are based.&lt;/p&gt;

&lt;p&gt;Currently, the CKEditor 5 component for Angular supports integrating CKEditor 5 only via builds. Integrating CKEditor 5 built from source is not possible yet due to the lack of ability to adjust webpack configuration in angular-cli.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While there is no support to integrate CKEditor 5 from source yet, you can still create a custom build of CKEditor 5 and include it in your Angular application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Quick start
&lt;/h1&gt;

&lt;p&gt;In your existing Angular project, install the CKEditor npm package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @ckeditor/ckeditor5-angular
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming that you picked @ckeditor/ckeditor5-build-classic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @ckeditor/ckeditor5-build-classic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, add CKEditorModule to modules whose components will be using the  component in their templates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { CKEditorModule } from '@ckeditor/ckeditor5-angular';

@NgModule( {
    imports: [
        CKEditorModule,
        // ...
    ],
    // ...
} )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import the editor build in your Angular component and assign it to a public property to make it accessible from the template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';

@Component( {
    // ...
} )
export class MyComponent {
    public Editor = ClassicEditor;
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, use the  tag in the HTML template to run the rich text editor:&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;ckeditor [editor]="Editor" data="&amp;lt;p&amp;gt;Hello, world!&amp;lt;/p&amp;gt;"&amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Integration with &lt;code&gt;ngModel&lt;/code&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create some model in your component to share with the editor:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component( {
    // ...
} )
export class MyComponent {
    public model = {
        editorData: '&amp;lt;p&amp;gt;Hello, world!&amp;lt;/p&amp;gt;'
    };
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Use the model in the template to enable a two–way data binding:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ckeditor [(ngModel)]="model.editorData" [editor]="Editor"&amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Supported &lt;code&gt;@Input&lt;/code&gt; properties
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Editor ( required )&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;The Editor which provides the static create() method to create an instance of the editor:&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;ckeditor [editor]="Editor"&amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Config&lt;/code&gt;:&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;ckeditor [config]="{ toolbar: [ 'heading', '|', 'bold', 'italic' ] }" &amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Data&lt;/code&gt;:&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;ckeditor data="&amp;lt;p&amp;gt;Hello, world!&amp;lt;/p&amp;gt;" &amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component( {
    // ...
} )
export class MyComponent {
    public editorData = '&amp;lt;p&amp;gt;Hello, world!&amp;lt;/p&amp;gt;';
    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ckeditor [data]="editorData" &amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Supported &lt;code&gt;@Output&lt;/code&gt; properties
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Change&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;It is fired with an object containing the editor and the CKEditor 5 &lt;code&gt;change:data&lt;/code&gt; event object.&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;ckeditor [editor]="Editor" (change)="onChange($event)"&amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { ChangeEvent } from '@ckeditor/ckeditor5-angular/ckeditor.component';

@Component( {
    // ...
} )
export class MyComponent {
    public Editor = ClassicEditor;

    public onChange( { editor }: ChangeEvent ) {
        const data = editor.getData();

        console.log( data );
    }
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting the placeholder
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ckeditor [config]="{placeholder: 'Type the content here!' }" &amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Accessing the editor instance
&lt;/h3&gt;

&lt;p&gt;To do this, create a template reference variable &lt;code&gt;#editor&lt;/code&gt; pointing to the  component:&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;ckeditor #editor [editor]="Editor"&amp;gt;&amp;lt;/ckeditor&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then get the &lt;code&gt;&amp;lt;ckeditor&amp;gt;&lt;/code&gt; component using a property decorated by &lt;code&gt;@ViewChild( 'editor' )&lt;/code&gt; and access the editor instance when needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component()
export class MyComponent {
    @ViewChild( 'editor' ) editorComponent: CKEditorComponent;

    public getEditor() {
        // Warning: This may return "undefined" if the editor is hidden behind the `*ngIf` directive or
        // if the editor is not fully initialised yet.
        return this.editorComponent.editorInstance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Thanks for reading my blog on how to add Rich Text Editor in angular, for more official in detail documentation please read &lt;a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/angular.html" rel="noopener noreferrer"&gt;CKEditor Angular&lt;/a&gt;.
&lt;/h2&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>ckeditor</category>
      <category>richtexteditor</category>
    </item>
    <item>
      <title>Implicit flow authentication using angular-oauth2-oidc (Angular)</title>
      <dc:creator>Deekshith Raj Basa 🔥</dc:creator>
      <pubDate>Tue, 22 Sep 2020 08:01:14 +0000</pubDate>
      <link>https://dev.to/deekshithrajbasa/implicit-flow-authentication-usingangular-oauth2-oidc-angular-2n90</link>
      <guid>https://dev.to/deekshithrajbasa/implicit-flow-authentication-usingangular-oauth2-oidc-angular-2n90</guid>
      <description>&lt;h3&gt;
  
  
  Installing the pacakage
&lt;/h3&gt;

&lt;p&gt;Install the angular-oauth2-oidc package using npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i angular-oauth2-oidc --save 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting up the NgModule (app.module)
&lt;/h3&gt;

&lt;p&gt;When package installation has been done then import the &lt;strong&gt;OAuthModule&lt;/strong&gt; in the app.modulefile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { OAuthModule } from 'angular-oauth2-oidc';
[...]
@NgModule({
  imports: [  
   [...]    HttpModule,
    OAuthModule.forRoot()  
],  
........export classAppModule{}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implicit Flow configuration &amp;amp; Login page
&lt;/h3&gt;

&lt;p&gt;This is the OAuth2/OIDC flow best suitable for SPA. It sends the user to the IdentityProvider's login page (Identity Server). After logging in, the SPA gets tokens. This alsoallows for single sign on as well as single sign off.&lt;br&gt;
To configure the library just have to set some properties (AuthConfig) on startup as requiredby OAuthService i.e. on the constructor of the AppComponent which is called before therouting kicks in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { AuthConfig } from'angular-oauth2-oidc';
export const authConfig: AuthConfig = {
// Url of the Identity Provider  
issuer: 'https://demo.identityserver.com/identity',
// Login Url of the Identity Provider  
loginurl: 'https://demo.identityserver.com/identity/connect/authorize',
// Login Url of the Identity Provider  
logouturl: 'https://demo.identityserver.com/identity/connect/endsession',
// URL of the SPA to redirect the user to after login  
redirectUri: window.location.origin + '/dashboard.html',
// The SPA's id. The SPA is registerd with this id at the auth-server  
clientId: 'billing_demo',
// set the scope for the permissions the client should request
// The first three are defined by OIDC. Also provide user sepecific
 scope: 'openid profile email billing_demo_api',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the AuthConfig properties has been set then configure the OAuthService for loadingidentity server's discovery document &amp;amp; if access token is invalid then trigger implicit flow for presenting users with the identity server's login page .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { OAuthService } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc';
import { authConfig } from './auth.config';
import { Component } from '@angular/core';
@Component({
  selector: 'billing-app',
  templateUrl: './app.component.html'
})
exportclassAppComponent{
  constructor(private oauthService: OAuthService) {
    this.ConfigureImplicitFlowAuthentication();
  } 
               private ConfigureImplicitFlowAuthentication() {
    this.oauthService.configure(authConfig);
    this.oauthService.tokenValidationHandler = 
new JwksValidationHandler();
    this.oauthService.loadDiscoveryDocument().then(doc) =&amp;gt; {
      this.oauthService.tryLogin().catch(err =&amp;gt; {
        console.error(err);
      }).then(() =&amp;gt; {
        if (!this.oauthService.hasValidAccessToken()) {
          this.oauthService.initImplicitFlow()
        }
      });
    });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The discovery endpoint can be used to retrieve metadata about your IdentityServer - it returns information like the issuer name, key material, supported scopes etc.The discovery endpoint is available via /.well-known/openid-configuration relative to the base address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://demo.identityserver.com/.well-known/openid-configuration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Automatically refreshing a token when/ before it expires (silent refresh)
&lt;/h3&gt;

&lt;p&gt;To automatically refresh a token when/ some time before it expires, just call the following method after configuring the OAuthService in the AppComponent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.oauthService.setupAutomaticSilentRefresh();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, this event is fired after 75% of the token's life time is over. You can adjust this factor by setting the property timeoutFactor to a value between 0 and 1. For instance, 0.5means, that the event is fired after half of the life time is over and 0.33 triggers the event after a third&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing Bearer access token when calling Web API
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Passing directly by using the class HttpHeaders of new HttpClient
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var headers = new HttpHeaders({"Authorization": "Bearer " + this.oauthService.getAccessToken()});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. By setting allowedUrls to an array with prefixes for the respective urls. Use lower case forthe prefixes.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OAuthModule.forRoot({
  resourceServer: {
    allowedUrls: ['https://billing_demo_api.com/api'],
    sendAccessToken: true
  }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logout configuration
&lt;/h3&gt;

&lt;p&gt;In order to log out from the application, just need to call the logout() method of theOAuthService. It will end the session &amp;amp; redirects the user to the post logout redirect url associated to the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.oauthService.logout();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Access Token &amp;amp; Identity Claims
&lt;/h3&gt;

&lt;p&gt;To get the access token &amp;amp; identity claims, just need to call the getAccessToken() andgetIdentityClaims() methods of the OAuthService&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.oauthService.getIdentityClaims();
this.oauthService.getAccessToken()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementing AuthGuard
&lt;/h3&gt;

&lt;p&gt;If you want to restrict some pages from unauthorized access, create AuthGuard class which further implements CanActivate that return either true if the user can access a route or false if they can’t based on the validity of the access token&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 { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';

@Injectable() export class AuthGuard implements CanActivate {
  constructor(private oauthService: OAuthService, private router: Router) {

  }
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (this.oauthService.hasValidAccessToken()) {
      return true;
    } this.router.navigate(['']);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Then add the AuthGuard to the routing configuation as
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  { path: 'home', component: HomeComponent },
  {
    path: 'search', component: SearchComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'bill/:billId', component: BillingComponent,
    canActivate: [AuthGuard], children: [
      { path: '', redirectTo: 'ledger' }
    ]
  }];

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

&lt;/div&gt;



</description>
      <category>angular</category>
      <category>authentication</category>
      <category>oauth2</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
