Angular ng-style background image when url contain spaces

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I'm trying to apply a background image to a div by using the angular ng-style and it works fine as long as the URL does not contain spaces.

ng-style="{'background-image': 'url(' + parentIMGLink + ')'}" 

When there is space in URL like parentIMGLink = servername.images/there is name.png it does not work. On the other hand:

img ng-src={{parentIMGLink }} 

Works fine. For sure I can manage so that names on the server wont contain spaces but is there any possibility to make ng-style work?

回答1:

Just like in css you have to wrap your the image url in quotes:

.rule{ background-image: url('/some/image url.png') } 

You have to do the same here:

<div ng-style="{'background-image': 'url(\'{{imageUrl}}\')'}"> 


回答2:

You can try this. In JS, replace " " with %20 (Percent-encoded space).

parentIMGLink = parentIMGLink.replace(" ", "%20"); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!