How to add background-image using ngStyle (angular2)?

Ignat picture Ignat · Jan 19, 2016 · Viewed 130.9k times · Source

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>

Answer

Thierry Templier picture Thierry Templier · Jan 19, 2016

I think you could try this:

<div [ngStyle]="{'background-image': 'url(' + photo + ')'}"></div>

From reading your ngStyle expression, I guess that you missed some "'"...