¿Cómo usar ngStyle para agregar una imagen de fondo? Mi código no funciona:
this.photo = 'http://dl27.fotosklad.org.ua/20121020/6d0d7b1596285466e8bb06114a88c903.jpg'; <div [ngStyle]="{'background-image': url(' + photo + ')}"></div>Creo que podrías intentar esto:
<div [ngStyle]="{'background-image': 'url(' + photo + ')'}"></div> Al leer tu expresión ngStyle , supongo que te perdiste algunos "'"...
También puedes probar esto:
[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>Ver también