sanitizing unsafe style value

meShell · 2019年06月12日 · 阅读 558

Example

<div class="header" *ngIf="image" [style.background-image]="image"></div>

Import DomSanitizer use bypassSecurityTrustStyle method.

export class StyleComponent implements OnInit {

  @Input() image: string;
  constructor(private sanitization: DomSanitizer) { }

  ngOnInit() {
    if (this.image) {
      this.image = this.sanitization.bypassSecurityTrustStyle(`url(${this.image})`);
    }
  }

}

关于作者

全栈工程师

文章被阅读 42.7k
获赞 3