First check whether image exists or not then load the image. This type of implementation gives clean UI view to user. In this example, initially we are loading image then assigning as background image if it exists.

Markup

<div id="divID" ng-click="setBackgroundImage($event,'http://lh3.googleusercontent.com/-GRRunt52GXo/AAAAAAAAAAI/AAAAAAAAAH4/Bz2Nf2zrXJk/s512-c/photo.jpg')">
 Sample DIV 1
</div>

Script

$scope.setBackgroundImage = function($event, imgLocation) {
    console.log('setBackgroundImage');
    var bgImg = new Image();
    bgImg.onload = function() {
        var myDiv = $event.target;
        myDiv.style.backgroundImage = 'url(' + bgImg.src + ')';
    };
    bgImg.src = imgLocation;
}

1 comment:

  1. Personally I think overjoyed I discovered the blogs.
    I would recommend my profile is important to me, I invite you to discuss this topic...
    background check mumbai

    ReplyDelete

Blogroll

Follow this blog by Email

Popular Posts