简体   繁体   中英

Angular ng-style background image when url contain spaces

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?

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}}\')'}">

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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