directive - angularjs: only set background-image on ngStyle if url is defined -
i have property on scope can hold url (bgurl) of image. use image background image of 'div'. however, if property undefined use default image defined class bg-image. can like
<img ng-show="bgurl" ng-src="bgurl"/> <div ng-hide="bgurl" class="bg-image"/>
however, combine 1 element. tried this
<div class="gb-image" ng-style="{true: 'background-image': 'url(\'' + bgurl + '\')'}[bgurl]"/>
i copied code google (no idea if can ever work). 1 thing clear though, doesn't work :) nice angular way solve , can solution work ?
right way directive way:
app.directive('bg', function () { return { link: function(scope, element, attrs) { if(scope.bgurl){ element.css("background-image","url("+scope.bgurl+")"); } else { element.addclass("bg-default"); } } } });
take look: http://plnkr.co/edit/xyejpp?p=preview
Comments
Post a Comment