AngularJS - ng-style does not update css background property
It doesn't work because angular evaluate the content of single bracket only once and doesn't locate the variable you want to bind inside that string.
You could however create your own directive as such:
app.directive('backImg', function(){
return function(scope, element, attrs){
attrs.$observe('backImg', function(value) {
element.css({
'background': 'transparent url(' + value +')'
});
});
};
});
and then use it in your template like this:
<div ng-if="vis" class="container" back-img="{{imgSrc}}"></div>
I have updated your fiddle and it seems to work fine http://jsfiddle.net/dS4pB/3/
you are probably facing same issue which I faced recently and solved using ng-bind .
check the answer explained in this question :
AngularJS : Why ng-bind is better than {{}} in angular?
once I used ng-bind my innerhtml is properly been updated.
I ran into this as well. Instead of evaluating with {{ }}
, use string + value concatenation.
This will work:
<span ng-style="{'background':'transparent url(' + item.img + ')'}"></span>)
Here's a working version of your fiddle
Where you are currently updating your scope variable, wrap this within a $timeout
call to push the updates to your scope and subsequently your view, this will effectively bump the update to the next digest
avoiding the digest already in progress conflict:
$timeout(function(){
$scope.var = value;
});
This question thread may give you some useful info regarding apply
vs timeout
etc