0

I tried putting background url for my image

 <div  [ngStyle]="{'background-image': 'url(' + https://s3-us-west-2.amazonaws.com/mysite/l/{{event.photo}} + ')'}"></div>

I am getting this error

Parser Error: Got interpolation ({{}}) where expression was expected 

Can anyone please hepl me.Thanks.

MMR
  • 2,869
  • 13
  • 56
  • 110

5 Answers5

1

I would suggest to build the styleString in the controller. The just use a simple variable in your code. Much cleaner this way.

Alexander_F
  • 2,831
  • 3
  • 28
  • 61
  • Fribu is undeniably right - you should keep your data out of view. If you don't want to change your manner this time, remove interpolation brackets like this: `'url(https://s3-us-west-2.amazonaws.com/mysite/l/' + event.photo + ')'` – mankers May 04 '17 at 13:17
1

Yor can't use property binding & interpolation together.

So try using the below code only property binding

<div [ngStyle]="{'background-image': 'url(https://s3-us-west-2.amazonaws.com/mysite/l/' + event.photo + ')'}"></div>

Hope this will help you.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
0

Try this instead:

<div  [ngStyle]="{'background-image': 'url('+'https://s3-us-west-2.amazonaws.com/mysite/l/'+event.photo+')'}"></div>

that works fine as of angular v4 and later

0

You can also use [style.background-image]

<div [style.background-image]="'url(https://s3-us-west-2.amazonaws.com/mysite/l/'+ event.photo +')'">
Admir
  • 2,876
  • 1
  • 19
  • 19
-1

I would like you verify above link to get better idea on background images

Angular2 dynamic background images

Attribute property binding for background-image url in Angular 2

Thank you

Community
  • 1
  • 1