Forcing a ng-src reload

前端 未结 5 821
生来不讨喜
生来不讨喜 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条回答
  •  广开言路
    2020-12-07 22:15

    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:

提交回复
热议问题