-
-
Save cengkuru/756a815872cd0c200cf198f108013f95 to your computer and use it in GitHub Desktop.
Angular 2 - Add safeHtml for innerHTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from '@angular/core'; | |
import { BrowserModule } from '@angular/platform-browser'; | |
import { SafeHtmlPipe } from "./pipes" | |
@NgModule({ | |
declarations: [ | |
SafeHtmlPipe, | |
], | |
imports: [ | |
], | |
providers: [ | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load html in Angular 2 | |
// In content.html use: | |
// <div [innerHTML]="post.content.rendered | safeHtml " class="entry-body"></div> | |
import { Pipe, PipeTransform } from "@angular/core"; | |
import { DomSanitizer } from "@angular/platform-browser"; | |
@Pipe({name: 'safeHtml'}) | |
export class SafeHtmlPipe implements PipeTransform { | |
constructor(private sanitized: DomSanitizer) { | |
} | |
transform(value: string) { | |
return this.sanitized.bypassSecurityTrustHtml(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment