强制ng-src重新加载

Rub*_*rop 75 angularjs

当图像的url没有改变但是其内容有?时,如何强制angularjs重新加载具有ng-src属性的图像?

<div ng-controller='ctrl'>
    <img ng-src="{{urlprofilephoto}}">
</div>
Run Code Online (Sandbox Code Playgroud)

执行文件上载的uploadReplace服务正在替换图像的内容,而不是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;
        }
    }
}]);
Run Code Online (Sandbox Code Playgroud)

当uploadReplace返回时,我不知道如何强制图像重新加载

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 
        })
    }
}])
Run Code Online (Sandbox Code Playgroud)

use*_*088 76

一个简单的解决方法是在ng-src中附加一个唯一的时间戳,以强制重新加载图像,如下所示:

$scope.$apply(function () {
    $scope.imageUrl = $scope.imageUrl + '?' + new Date().getTime();
});
Run Code Online (Sandbox Code Playgroud)

要么

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;
}]);
Run Code Online (Sandbox Code Playgroud)


Phi*_*ley 27

也许它可以像在图像URL中添加decache查询字符串一样简单?即.

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

这应该强制它重新加载.

  • 谢谢.基本上添加任何查询参数应该可以解决问题 (3认同)

acr*_*omm 16

"角度方法"可以创建自己的过滤器,以将随机查询字符串参数添加到图像URL.

像这样的东西:

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

然后你可以像这样使用它:

<img ng-src="{{yourImageUrl | randomSrc}}" />
Run Code Online (Sandbox Code Playgroud)


Nit*_*mar 6

试试这个

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.
    });
}
 }])
Run Code Online (Sandbox Code Playgroud)