Sekrab Garage

Angular security undocumented

Sanitizing background image url in Angular

AngularTip December 1, 20
Subscribe to Sekrab Parts newsletter.

According to Angular documentation, you need to sanitize urls before you use them, one way mentioned is bypassSecurityTrustStyle, but they don't tell you how to use it. This is how:

this.bgstyle = this.sanitizer.bypassSecurityTrustStyle(`url("${imageUrl}")`);

Sanitize the style value, including "url". And in HTML template:

<div [style.background-image]="bgStyle" ></div>