angularjs ng-style: background-image isn't working

后端 未结 8 1135
一整个雨季
一整个雨季 2020-12-12 20:38

I\'m trying to apply a background image to a div by using the angular ng-style ( I tried a custom directive before with the same behaviour ), but it doesn\'t se

8条回答
  •  失恋的感觉
    2020-12-12 20:58

    If we have a dynamic value that needs to go in a css background or background-image attribute, it can be just a bit more tricky to specify.

    Let’s say we have a getImage() function in our controller. This function returns a string formatted similar to this: url(icons/pen.png). If we do, the ngStyle declaration is specified the exact same way as before:

    ng-style="{ 'background-image': getImage() }"
    

    Make sure to put quotes around the background-image key name. Remember, this must be formatted as a valid Javascript object key.

提交回复
热议问题