How to use ngStyle to add background-image? My code doesn't work:
this.photo = 'http://dl27.fotosklad.org.ua/20121020/6d0d7b1596285466e8bb06114a88c903.jpg';
<div [ngStyle]="{'background-image': url(' + photo + ')}"></div>
I think you could try this:
<div [ngStyle]="{'background-image': 'url(' + photo + ')'}"></div>
From reading your ngStyle expression, I guess that you missed some "'"...
Also you can try this:
[style.background-image]="'url(' + photo + ')'"
import {BrowserModule, DomSanitizer} from '@angular/platform-browser'
constructor(private sanitizer:DomSanitizer) {
this.name = 'Angular!'
this.backgroundImg = sanitizer.bypassSecurityTrustStyle('url(http://www.freephotos.se/images/photos_medium/white-flower-4.jpg)');
}
<div [style.background-image]="backgroundImg"></div>
See also