# Inserting HTML via Angular 2: Use of DomSanitizationService & bypassSecurityTrustHtml

**URL:** <https://forum.ionicframework.com/t/inserting-html-via-angular-2-use-of-domsanitizationservice-bypasssecuritytrusthtml/62562>\
**Category:** ionic-v3\
**Created:** [September 5, 2016, 4:25am UTC](https://forum.ionicframework.com/t/inserting-html-via-angular-2-use-of-domsanitizationservice-bypasssecuritytrusthtml/62562 "2016-09-05T04:25:53Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![infoproducts](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/infoproducts/32/100372_2.png) [@infoproducts](https://forum.ionicframework.com/u/infoproducts)\
**Post date:** [September 6, 2016, 1:38pm UTC](https://forum.ionicframework.com/t/inserting-html-via-angular-2-use-of-domsanitizationservice-bypasssecuritytrusthtml/62562/2 "2016-09-06T13:38:26Z")

</div>

I recently created a pipe to make it simpler to mark values as trusted/safe.

You can use it to bypass html sanitization like this in templates:

```auto
<div [innerHTML]="product.description | safe: 'html'"></div>

```

Or if you need to bypass style sanitization:

```auto
<div [style.background-image]="'url(' + product.imageUrl + ')' | safe: 'style'"></div>

```

‘script’, ‘url’ and ‘resourceUrl’ is also supported. Read more here: [https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizationService-class.html](https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizationService-class.html)

Here’s the SafePipe code:

```auto
import {Pipe} from '@angular/core';
import {DomSanitizationService, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl} from '@angular/platform-browser';

@Pipe({
	name: 'safe'
})
export class SafePipe {

	constructor(protected _sanitizer: DomSanitizationService) {

	}

	public transform(value: string, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
		switch (type) {
			case 'html':
				return this._sanitizer.bypassSecurityTrustHtml(value);
			case 'style':
				return this._sanitizer.bypassSecurityTrustStyle(value);
			case 'script':
				return this._sanitizer.bypassSecurityTrustScript(value);
			case 'url':
				return this._sanitizer.bypassSecurityTrustUrl(value);
			case 'resourceUrl':
				return this._sanitizer.bypassSecurityTrustResourceUrl(value);
			default:
				throw new Error(`Unable to bypass security for invalid type: ${type}`);
		}
	}

}

```

---

_[View the full topic](https://forum.ionicframework.com/t/inserting-html-via-angular-2-use-of-domsanitizationservice-bypasssecuritytrusthtml/62562)._
