<?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: chhkas1</title>
    <description>The latest articles on DEV Community by chhkas1 (@chhkas1).</description>
    <link>https://dev.to/chhkas1</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%2F1260953%2F301b52d2-b1ec-487f-a98d-75a44fc0d8d1.jpg</url>
      <title>DEV Community: chhkas1</title>
      <link>https://dev.to/chhkas1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chhkas1"/>
    <language>en</language>
    <item>
      <title>A short tutorial on how to render html inside html in Angular</title>
      <dc:creator>chhkas1</dc:creator>
      <pubDate>Fri, 02 Feb 2024 07:17:17 +0000</pubDate>
      <link>https://dev.to/chhkas1/a-short-tutorial-on-how-to-render-html-inside-html-in-angular-3hli</link>
      <guid>https://dev.to/chhkas1/a-short-tutorial-on-how-to-render-html-inside-html-in-angular-3hli</guid>
      <description>&lt;p&gt;In this tutorial, you will learn &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how to render html inside html in angular&lt;/li&gt;
&lt;li&gt;The use of sanitizer in rendering html in angular.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Angular, you can render HTML within an HTML tag using the &lt;code&gt;innerHTML&lt;/code&gt; property. You can bind the HTML content to a variable and then use this variable within the HTML tag. Here's an example:&lt;/p&gt;

&lt;p&gt;In your component.ts file:&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="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-example&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;div [innerHTML]="htmlContent"&amp;gt;&amp;lt;/div&amp;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;ExampleComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;htmlContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;p&amp;gt;This is the rendered HTML content&amp;lt;/p&amp;gt;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using the &lt;code&gt;innerHTML&lt;/code&gt; property binding, you can render HTML content within the specified HTML tag in Angular. Always be cautious with this approach especially if the HTML comes from an untrusted source to avoid cross-site scripting (XSS) vulnerabilities.&lt;/p&gt;

&lt;p&gt;In Angular, you can use the DomSanitizer service to sanitize and securely render HTML content. This is crucial for preventing cross-site scripting (XSS) vulnerabilities when rendering user-provided or untrusted HTML. Here's how you can use the &lt;code&gt;DomSanitizer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First, you need to &lt;code&gt;import DomSanitizer from '@angular/platform-browser'&lt;/code&gt; in your component file.&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="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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DomSanitizer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SafeHtml&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/platform-browser&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-example&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;div [innerHtml]="sanitizedHtml"&amp;gt;&amp;lt;/div&amp;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;ExampleComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;sanitizedHtml&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SafeHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&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;sanitizer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DomSanitizer&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;untrustedHtml&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;script&amp;gt;alert("This is a malicious script!")&amp;lt;/script&amp;gt;&lt;/span&gt;&lt;span class="dl"&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;sanitizedHtml&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;sanitizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bypassSecurityTrustHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;untrustedHtml&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 example, we import &lt;code&gt;DomSanitizer&lt;/code&gt; and &lt;code&gt;SafeHtml&lt;/code&gt;. We then inject &lt;code&gt;DomSanitizer&lt;/code&gt; into the component's constructor. We use &lt;code&gt;bypassSecurityTrustHtml()&lt;/code&gt; method of &lt;code&gt;DomSanitizer&lt;/code&gt; to securely render the untrusted HTML by bypassing Angular's built-in security. Finally, we bind the sanitized HTML to the &lt;code&gt;innerHtml&lt;/code&gt; property of a HTML tag within the component's template.&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;DomSanitizer&lt;/code&gt;, you can mitigate the risk of XSS attacks when rendering untrusted HTML in your Angular application. Always be careful when dealing with untrusted HTML and consider other security measures as well.&lt;/p&gt;

&lt;p&gt;It is highly recommended to sanitize your HTML while rendering to keep it protected.&lt;br&gt;
If you find this helpful, please drop your comment &lt;/p&gt;

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