Forcing a ng-src reload

前端 未结 5 797
生来不讨喜
生来不讨喜 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:

    <img ng-src="{{yourImageUrl | randomSrc}}" />

    0 讨论(0)
  • 2020-12-07 22:16

    I resorted to make a directive to put random param in the src, but only if the image changes, so I don't mess that much with the caching.

    I use it to update the user's profile pic in the navbar when they update it via AJAX, which doesn't happen that often.

    (function() {
      "use strict";
    
      angular
        .module("exampleApp", [])
        .directive("eaImgSrc", directiveConstructor);
    
      function directiveConstructor() {
        return { link: link };
    
        function link(scope, element, attrs) {
          scope.$watch(attrs.eaImgSrc, function(currentSrc, oldSrc) {
            if (currentSrc) {
              // check currentSrc is not a data url,
              // since you can't append a param to that
              if (oldSrc && !currentSrc.match(/^data/)) {
                setSrc(currentSrc + "?=" + new Date().getTime());
              } else {
                setSrc(currentSrc);
              }
            } else {
              setSrc(null);
            }
          })
    
          function setSrc(src) { element[0].src = src; }
        }
      }
    })();
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="exampleApp">
      <div>
        <img ea-img-src="src"></img>
      </div>
    
      <button ng-click="src = 'http://placehold.it/100x100/FF0000'">IMG 1</button>
      <button ng-click="src = 'http://placehold.it/100x100/0000FF'">IMG 2</button>
      <button ng-click="src = 'http://placehold.it/100x100/00FF00'">IMG 3</button>
    </div>

    0 讨论(0)
  • 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;
    }]);
    
    0 讨论(0)
  • 2020-12-07 22:32

    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.
        });
    }
     }])
    
    0 讨论(0)
  • 2020-12-07 22:41

    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.

    0 讨论(0)
提交回复
热议问题