简体   繁体   English

如何将背景图像放入角度2

[英]How to put background image in angular 2

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. 谁能帮助我。谢谢。

I would suggest to build the styleString in the controller. 我建议在控制器中建立styleString。 The just use a simple variable in your code. 只需在代码中使用一个简单变量即可。 Much cleaner this way. 这样更清洁。

Yor can't use property binding & interpolation together. 您不能同时使用属性绑定和插值。

So try using the below code only property binding 因此,请尝试仅使用以下代码进行property binding

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

Hope this will help you. 希望这会帮助你。

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 从角度v4起可以正常使用

您也可以使用[style.background-image]

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

I would like you verify above link to get better idea on background images 我希望您验证上面的链接以更好地了解背景图片

Angular2 dynamic background images Angular2动态背景图片

Attribute property binding for background-image url in Angular 2 Angular 2中背景图片网址的属性属性绑定

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM