Reload image in Angular

I try to reload the image from my web server every n seconds. The code below drags a new image, but it does not replace the old one in the client. Where is my mistake?

<div ng-app="refresh-div" ng-controller="refresh_control">
    <img ng-src="{{imageUrl}}" />
</div>

<script>
    var app = angular.module('refresh-div', [])
        .controller('refresh_control', function ($scope, $http, $timeout) {
            $scope.imageURL = 'assets/now.png?_ts=' + new Date().getTime();

            $scope.getData = function () {
                $http.get($scope.imageURL, {
                    cache: false
                }).success(function (data, status, headers, config) {
                    $scope.image = data;
                    $scope.imageUrl = "http://mywebsite/assets/now.png";
                });
            };

            $scope.intervalFunction = function () {
                $timeout(function () {
                    $scope.getData();
                    $scope.intervalFunction();
                }, 1500)
            };

            $scope.intervalFunction();
        });
</script>
+4
source share
2 answers

Bug fixed, the problem was that $ scope.imageURL = ' http: //mywebsite/assets/now.png? _Ts = ' + new Date (). getTime (); absent in the function of success. Basically, I needed to update the image URL. I'm not sure if this is the best solution, but it works for my requirements.

<div ng-app="refresh-div" ng-controller="refresh_control">
    <img ng-src="{{imageURL}}" />
</div>

<script>
    var app = angular.module('refresh-div', [])
        .controller('refresh_control', function ($scope, $http, $timeout) {
            $scope.imageURL = 'http://mywebsite/assets/now.png?_ts=' + new Date().getTime(); 

            $scope.getImage = function () {
                $http.get($scope.imageURL, {
                    cache: false
                }).success(function (data, status, headers, config) {
                    $scope.imageURL = 'http://mywebsite/assets/now.png?_ts=' + new Date().getTime();
                });
            };

            $scope.intervalFunction = function () {
                $timeout(function () {
                    $scope.getImage();
                    $scope.intervalFunction();
                }, 1500)
            };

            $scope.intervalFunction();
        });
</script>
+2
source

a) <img ng-src="{{imageUrl}}" /> - it force Angular.js $scope.imageUrl. Angular .

)

$scope.getData = function () {
  $http.get($scope.imageURL, {
    cache: false
})
.success(function (data, status, headers, config) {
  $scope.image = data;
  $scope.imageUrl = "http://mywebsite/assets/now.png"; // <== this line
});

$scope.imageUrl = "http://mywebsite/assets/now.png"; $scope.imageUrl. .

,

$scope.intervalFunction = function () {
  $timeout(function () {
    $scope.imageURL = 'assets/now.png?_ts=' + new Date().getTime();
  }, 1500);
};

?

+2

All Articles