Display raw html - Angular 6

angular 6 bind html
angular render html
angular interpolation display html
append html in angular 6
ng-bind-html equivalent in angular 7
angular 4 display html from variable
remove html tags in angular 6
angular 6 parse html string

I am looking to display the raw HTML code (example.component.html) below 'example works!'. The page should display the following:

example works!    
  example works!

I can find various resources showing how this can be done using AngularJS but not Angular 6.

I have tried to use [innerHTML] but this didn't work.

  example works!


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

  selector: 'rt-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
export class ExampleComponent implements OnInit {

  constructor() { }

  ngOnInit() {



<div style="text-align:center">

OUTPUT . . link to image

You can use the ViewContainerRef to get the nativeElement, and get its innerHTML.

Just a warning : this won't be your HTML code, but the compiled code.

Here is an example : stackblitz

export class AppComponent  {
  htmlContent: string;
  constructor(private view: ViewContainerRef) {
    setTimeout(() => this.htmlContent = (view.element.nativeElement as HTMLElement).innerHTML);

EDIT If you want the uncompiled code, you should use this notation : stackblitz

import { Component, ViewContainerRef } from '@angular/core';
import * as template from "./app.component.html";

  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
export class AppComponent  {
  htmlContent: string = template.default;
  constructor(private view: ViewContainerRef) {

But I suggest you use the latest versions of typescript & Angular, since I'm not sure when it has been introduced.

How to bind raw html in Angular2, but the text in myField will get escaped. For Angular 1.x i found hits for ng-bind-​html, but this seems not be supported in 2.x. thx Frank. Safe HTML. HTML data sometimes can contain malicious content - be wary of situations when unknown HTML data needs to be processed. Luckily, Angular comes with built-in XSS protection and a separate DomSanitizer which can be used to help prevent Cross-Site Scripting Security (XSS) bugs.

Get a reference pointing to that component using ViewChild, like

export class AppComponent  {
  @ViewChild(ExampleComponent, { read: ElementRef }) exampleComponent;

Then you can access its HTML with nativeElement.innerHTML.

So you could change your app.component.html to this:

<div style="text-align:center">


Display raw html - Angular 6, Change this: export class ComponentOne to this: export class ComponentOne implements OnInit and do not forget to import: import { Component, OnInit, Input} from '#angular/core'; In your code, change String to string in all the places shown in the question. And see if it works. String is a constructor for type string. Preserve HTML in text output with AngularJS 1.1 and AngularJS 1.2+. In this quick tip I will show you how you can preserve your HTML output with AngularJS. By default, AngularJS will turn your HTML into normal text, in other words: make it safe so that by default people can’t insert any unwanted HTML into your page.

In HTML, you need to use entities to show the reserved characters.

<p>example works!</p>

<code>&lt;p&gt;example works!&lt;/p&gt;</code>

For example:

&amp; is equivalent to &

&lt; is equivalent to <

&gt; is equivalent to >

However solution using angular would be:


export class SomeComponent  {
  code :string = `<p>example</p>`



OR Use InnerText instead of InnerHTML

<p [innerText]="code"></p>


Angular 2: Display HTML without sanitizing/filtering, You might have a situation where you need to display/bind HTML code into some DOM elements. Usually, there's no problem in that binding, except that Angular  The ng-bind-html directive is a secure way of binding content to an HTML element. When you are letting AngularJS write HTML in your application, you should check the HTML for dangerous code. By including the "angular-sanitize.js" module in your application you can do so by running the HTML code through the ngSanitize function.

Make sure you've added your new component to app.module.ts. To know what and where you should add after create new component, yo can use angularCLI:

ng g c yourComponentName

this will generate a new component called "yourComponentName" and update app.module.ts file, so you can use it.

Binding HTML with Angular, In this article we are reviewing what it takes to render HTML content inside we wish to display inside a <div> element in an Angular template. You display your data to a user (and collect data from the user) by binding controls in the HTML template to the data properties of the component class. In addition, you can add logic to the template by including directives, which tell Angular how to modify the page as it is rendered.

String with html is rendering as raw html in Safari · Issue #208 , These render correctly as hyperlinks in all browsers but Safari. Angular CLI: 6.0​.8 Node: 10.1.0 OS: darwin x64 Angular: undefined . Ever had properties in your component that contain some html markup or entities that you need to display in your template? The traditional interpolation won’t work, but the innerHTML property binding comes to the rescue. This post covers Angular 2 and up . Given a property in our component like this:

innerHTML Property Binding in Angular ← Alligator.io, Ever had properties in your component that contain some html markup or entities that you need to display in your template? The traditional  Angular HTML binding (12) I apologize if I am missing the point here, but I would like to recommend a different approach: I think it's better to return raw data from your server side application and bind it to a template on the client side.

Angular raw html, Reactive forms is an Angular 6 technique for creating forms in a reactive style. If you need to display raw HTML content to the page view with AngularJS, you  AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!

  • @ritaj at the moment the text 'example works!' is displayed but I would like to add the raw html code below it. <p> example works! </p>
  • Possible duplicate of Angular 4 Output Complete HTML Syntax code in HTML as raw text
  • that's really useful, thanks but I'm looking for the HTML code rather than the compiled code.
  • I have edited the code and added the example component stackblitz.com/edit/angular-jgelqd but I think I have made an error. The HTML code at the bottom isn't right.
  • I have added an image link in the question for clarity to give a better idea of what I'm looking for.... I would like the HTML code at the bottom to resemble that of the code in example.component.html without the <div class="markup">{{ htmlContent }}</div> . Is this possible?
  • No it's not. For that, you'll need two components. You can't just pick what you like in a template. If you want to do that, simply rewrite your HTML.
  • Not wrong, but ViewContainerRef allows him to get directly the host component (the <my-app> tag directly)
  • @rf2018 tag <code> is optional. You can use other tags too.