Forcing a ng-src reload

前端 未结 5 818
生来不讨喜
生来不讨喜 2020-12-07 21:59

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?

5条回答
  •  猫巷女王i
    2020-12-07 22:23

    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;
    }]);
    

提交回复
热议问题