问题
I\'ve been using DomSanitizer with an SVG in an html string.
Previous to the current version of Angular, this worked just fine:
this.domSanitizer.bypassSecurityTrustHtml(content);
Now I am getting an object back called
SafeHtmlImpl {changingThisBreaksApplicationSecurity: \"<svg> blah </svg>\"}
changingThisBreaksApplicationSecurity
Is there now a new way to access the output of the DomSanitizer? Should I be receiving it as SafeHTML type or something? What\'s the point in having bypassSecurityTrustHtml if it still filters html?
Any answers on a postcard? Please...
回答1:
DEMO : https://plnkr.co/edit/Qke2jktna55h40ubUl8o?p=preview
import { DomSanitizer } from '@angular/platform-browser'
@Pipe({ name: 'safeHtml'})
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
transform(value) {
console.log(this.sanitized.bypassSecurityTrustHtml(value))
return this.sanitized.bypassSecurityTrustHtml(value);
}
}
@Component({
selector: 'my-app',
template: `
<div [innerHtml]="html | safeHtml">
</div>
`,
})
export class App {
name:string;
html: safeHtml;
constructor() {
this.name = 'Angular2'
this.html = "<svg> blah </svg>";
}
}
回答2:
Use DomSanitizer.bypassSecurityTrustHtml
:
constructor(private sanitizer: DomSanitizer) {
}
let html = this.sanitizer.bypassSecurityTrustHtml("<svg> blah </svg>");
More information: https://angular.io/docs/ts/latest/guide/security.html#bypass-security-apis
来源:https://stackoverflow.com/questions/39857858/angular-2-domsanitizer-bypasssecuritytrusthtml-svg