Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to show html string in angular template

How to show html string in angular template ?

Aman Jain, March 31, 2022March 31, 2022

Angular built-in supports vulnerabilities and attacks in a web application like cross site scripting, however sometimes in our application we want to render the html string through bypassing the security of angular.

To bypass the security, angular itself provides some methods and directives by which we can render or show html string in angular.

There is two ways to render the html string

  1. innerHTML directive – Render only simple html tags and style tags, and remove style attributes and script tag
  2. DomSanitizer – Render html with style tag and style tags but also remove scripts

Simple syntax for innerHTML

<div [innerHTML]="html"></div>

Simple syntax for DomSanitizer

 this.safehtmlStr=dom.bypassSecurityTrustHtml(this.html);

Example 1 : innerHTML to render the string

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'newapp';
  html:string="<p style='color:red'>String with color:red </p><a href='https://google.com'>google.com</a><script>alert('aa')</script>"
 
  constructor(){
     
  }
}

and html


<h2>Simple HTML only without style(Removed)</h2>
<div [innerHTML]="html"></div>

Example 2 : innerHTML to render the string

import { Component } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'newapp';
  html:string="<style>a{ color:green}</style><p style='color:red'>String with color:red </p><a href='https://google.com'>google.com</a><script>alert('aa')</script>"
  safehtmlStr:SafeHtml;

  constructor( public dom:DomSanitizer){
      this.safehtmlStr=dom.bypassSecurityTrustHtml(this.html);
  }
}

and html

<h2> HTML With style</h2>

<div [innerHTML]="safehtmlStr"></div>

Output :

Screenshot 2022 03 31 at 11.14.21 PM
show html string in angular

As you can see we have used here DomSanitizer and its method bypassSecurityTrustHtml to sanitize the html string but it removes the script tag and only keeps html and inline style.

Create a pipe for safe html for global use

we can also create a pipe for safe html so we can use it globally in our app as below

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Pipe({
  name: 'safehtml'
})
export class SafePipe implements PipeTransform {

  constructor(protected sanitizer: DomSanitizer) {}
 
 public transform(value: any): SafeHtml {
      return this.sanitizer.bypassSecurityTrustHtml(value);
  }
}

and in html


<h2>HTML with pipe</h2>
<div [innerHTML]="html | safehtml"></div>

Live Example:

Also Read : How to Insert script tag dynamically in angular ?

Related

Javascript Angular angularinnerHTML

Post navigation

Previous post
Next post

Related Posts

Javascript Angular Keypress Enter Event on Input Example

Angular Keypress Enter Event on Input Example ?

September 21, 2022March 16, 2024

Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Keypress Enter Event on Input with Example. In core javascript we use onkeypress event to handle the key events of input but in angular it there is different way to…

Read More
Javascript How to copy object without reference in angular

How to copy object without reference in angular ?

April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original…

Read More
Angular How to Use Interface in Angular with Example

How to Use Interface in Angular with Example?

August 6, 2022March 16, 2024

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version