msqar msqar - 10 months ago 67
Javascript Question

Add a background or not depending on angularjs condition using ng-style

I'm trying to add a background url style depending on a condition, so I attemped using data-ng-style but couldn't make it work for some reason, i don't know what I'm doing wrong.

data-ng-style="true : {'background-image': 'url(' + getThumbnailUrl(item) + ')'}, false : {}[item.thumbnailClass === undefined]"

Basically, what I want to do is, if thumbnailClass is undefined, set the background-image as data-ng-style, otherwise, do nothing.

Adding JSFiddle
Imagine even that fafa is boolean, if true apply the style, if false, dont do anything.

How can I achieve this in AngularJS ? Thanks.


Your syntax is just a bit off. This should work.

data-ng-style="{'background-image': (item.thumbnailClass === undefined) ? 'url(' + getThumbnailUrl(item) + ')' : ''}"