{"id":1133,"date":"2021-05-04T14:53:06","date_gmt":"2021-05-04T12:53:06","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1133"},"modified":"2021-11-04T11:10:12","modified_gmt":"2021-11-04T10:10:12","slug":"angular-displaying-html-content-without-sanitizing-filtering","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/05\/04\/angular-displaying-html-content-without-sanitizing-filtering\/","title":{"rendered":"Angular: displaying HTML content without sanitizing\/filtering"},"content":{"rendered":"\n<p>You might have a situation where you need to display\/bind HTML code into some DOM elements.<\/p>\n\n\n\n<p>Usually, there\u2019s no problem in that binding,<span class=\"has-inline-color has-vivid-cyan-blue-color\"> except that Angular  will remove all the attributes from your HTML tags, and display them as simple string. <\/span><\/p>\n\n\n\n<p>One solution to this is to create a Pipe and use it whenever you need the HTML to be as is (not filtered, not sanitized).<\/p>\n\n\n\n<p class=\"has-normal-font-size\"><strong><span class=\"has-inline-color has-vivid-red-color\">1 .<\/span><\/strong>  First create new file to hold the pipe&nbsp;<code>pipes\/keep-html.pipe.ts<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g pipe &lt;name&gt; <\/code><\/pre>\n\n\n\n<p>the pipe ts.file will be filled like this : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Pipe, PipeTransform } from '@angular\/core';\nimport { DomSanitizer } from '@angular\/platform-browser';\n\n@Pipe({ name: 'safeHtml', pure: false })\nexport class SafeHtmlPipe implements PipeTransform {\n  constructor(private sanitizer: DomSanitizer) {\n  }\n\n  transform(content) {\n    return this.sanitizer.bypassSecurityTrustHtml(content);\n  }\n}<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">2.<\/span><\/strong>  An import statement should be added (trough &#8216;ng generate&#8217; function)  in your&nbsp;<code><strong>app.module.ts<\/strong><\/code>.  If not, you have to do it yourself. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { SafeHtmlPipe } from '.\/pipes\/safe-html.pipe';\n@NgModule({\n  declarations: &#91;\n    SafeHtmlPipe\n  ],\n  bootstrap: &#91;AppComponent]\n})\nexport class AppModule {\n}<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">3. <\/span><\/strong> Finally use that pipe in your template:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div &#91;innerHTML]=\"htmlContent | safeHtml\"&gt;&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You are done !<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/medium.com\/\">https:\/\/medium.com\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/37076867\/in-rc-1-some-styles-cant-be-added-using-binding-syntax\/37076868#37076868\">https:\/\/stackoverflow.com\/questions\/37076867\/in-rc-1-some-styles-cant-be-added-using-binding-syntax\/37076868#37076868<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might have a situation where you need to display\/bind HTML code into some DOM elements. Usually, there\u2019s no problem in that binding, except that Angular will remove all the attributes from your HTML tags, and display them as simple string. One solution to this is to create a Pipe and use it whenever you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1365,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,83],"tags":[303,305,306,304],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1133"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1133"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1133\/revisions"}],"predecessor-version":[{"id":1366,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1133\/revisions\/1366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1365"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}