javascript - Using Angular 2/4 [innerHTML] to add html including style attributes -
i'm using angular 2/4 [innerhtml] input insert html formatting including style tags.
in template have like:
<span [innerhtml]="somevar"></span> in component, have:
somevar = `<span style="background-color:#990000">test</span>`; i warning:
warning: sanitizing html stripped content (see http://g.co/ng/security#xss). in output, inserted span in intact, minus style attribute.
so used pipe post:
https://stackoverflow.com/questions/37076867/
it looks like:
import { pipe, pipetransform } '@angular/core'; import { domsanitizer, safehtml } '@angular/platform-browser'; @pipe({name: 'safehtml'}) export class sanitizehtml implements pipetransform { constructor(private sanitizer: domsanitizer) {} transform(html): safehtml { // return this.sanitizer.bypasssecuritytruststyle(style); return this.sanitizer.bypasssecuritytrusthtml(html); // return this.sanitizer.bypasssecuritytrustscript(value); // return this.sanitizer.bypasssecuritytrusturl(value); // return this.sanitizer.bypasssecuritytrustresourceurl(value); } } this yields no difference before, though i'm not sure i'm using right way...
how can angular retain style attribute using innerhtml?
you're there. need make sure using pipe html string.
example pipe:
import {pipe} '@angular/core'; import {domsanitizer, safehtml, safestyle, safescript, safeurl, saferesourceurl} '@angular/platform-browser'; @pipe({ name: 'safe' }) export class safepipe { constructor(protected sanitizer: domsanitizer) {} transform(htmlstring: string): { return this.sanitizer.bypasssecuritytrusthtml(htmlstring); } } example usage:
<span [innerhtml]="somevar | safe"></span> hope helps!
Comments
Post a Comment