Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline styling in Angular2

Tags:

angular

I am getting blocks of HTML codes from HTTP calls which has inline styling in it. I put the HTML blocks in a variable and insert it on my page with [innerHTML] but I cannot see the style reflected in the inserted HTML block. Does anyone have any suggestion how I can achieve this?

@Component({
  selector: 'my-app',
  template: `
    <input type="text" [(ngModel)]="html">
    <div [innerHtml]="html">
    </div>
})
export class App {
  name:string;
  html: string;
  constructor() {
    this.name = 'Angular2'
    this.html = "<span style=\"color:red;\">1234</span>";
  }
}

In the above example 1234 is not coming red.

Here is the plnkr

like image 461
iniravpatel Avatar asked Mar 09 '23 16:03

iniravpatel


1 Answers

  constructor(private sanitizer:DomSanitizer) {
    this.html = sanitizer.bypassSecurityTrustHtml("<span style=\"color:red;\">1234</span>");

Plunker example

See also

  • https://angular.io/docs/ts/latest/api/platform-browser/index/DomSanitizer-class.html
  • In RC.1 some styles can't be added using binding syntax
like image 141
Günter Zöchbauer Avatar answered Mar 19 '23 03:03

Günter Zöchbauer