Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a ng-src reload

Tags:

angularjs

How can I force angularjs to reload an image with an ng-src attribute, when the url of the image has not changed, but its contents has?

<div ng-controller='ctrl'>
    <img ng-src="{{urlprofilephoto}}">
</div>

An uploadReplace service that performs a file upload, is replacing the content of the image, but not the url.

app.factory('R4aFact', ['$http', '$q', '$route', '$window', '$rootScope',
function($http, $q, $route, $window, $rootScope) {
    return {
        uploadReplace: function(imgfile, profileid) {
            var xhr = new XMLHttpRequest(),
                fd = new FormData(),
                d = $q.defer();
            fd.append('profileid', profileid);
            fd.append('filedata', imgfile);
            xhr.onload = function(ev) {
                var data = JSON.parse(this.responseText);
                $rootScope.$apply(function(){
                    if (data.status == 'OK') {
                        d.resolve(data);
                    } else {
                        d.reject(data);
                    }
                });
            }
            xhr.open('post', '/profile/replacePhoto', true)
            xhr.send(fd)
            return d.promise;
        }
    }
}]);

When the uploadReplace returns, I don't know how I can force the image to reload

app.controller('ctrl', ['$scope', 'R4aFact', function($scope, R4aFact){
    $scope.clickReplace = function() {
        R4aFact.uploadReplace($scope.imgfile, $scope.pid).then(function(){
            // ??  here I need to force to reload the imgsrc 
        })
    }
}])
like image 900
Ruben Decrop Avatar asked Sep 17 '13 08:09

Ruben Decrop


4 Answers

An easy workaround is to append a unique timestamp to ng-src to force image reload as follows:

$scope.$apply(function () {
    $scope.imageUrl = $scope.imageUrl + '?' + new Date().getTime();
});

or

angular.module('ngSrcDemo', [])
    .controller('AppCtrl', ['$scope', function ($scope) {
    $scope.app = {
        imageUrl: "http://example.com/img.png"
    };
    var random = (new Date()).toString();
    $scope.imageSource = $scope.app.imageUrl + "?cb=" + random;
}]);
like image 187
user225088 Avatar answered Nov 14 '22 14:11

user225088


Perhaps it could be as simple as adding a decache query string to the image URL? ie.

var imageUrl = 'http://i.imgur.com/SVFyXFX.jpg';
$scope.decachedImageUrl = imageUrl + '?decache=' + Math.random();

This should force it to reload.

like image 21
Philip Bulley Avatar answered Nov 14 '22 15:11

Philip Bulley


An "angular approach" could be creating your own filter to add a random querystring parameter to the image URL.

Something like this:

.filter("randomSrc", function () {
    return function (input) {
        if (input) {
            var sep = input.indexOf("?") != -1 ? "&" : "?";
            return input + sep + "r=" + Math.round(Math.random() * 999999);
        }
    }
})

Then you can use it like this:

<img ng-src="{{yourImageUrl | randomSrc}}" />
like image 16
acromm Avatar answered Nov 14 '22 16:11

acromm


Try This

app.controller('ctrl', ['$scope', 'R4aFact', function($scope, R4aFact){
$scope.clickReplace = function() {
    R4aFact.uploadReplace($scope.imgfile, $scope.pid).then(function(response){
        $scope.urlprofilephoto  = response + "?" + new Date().getTime(); //here response is ur image name with path.
    });
}
 }])
like image 6
Nitish Kumar Avatar answered Nov 14 '22 15:11

Nitish Kumar