I have an Angular application which shows people profile images(loaded from a server) and has an option to change it.
When the application loads the first time, the image is displayed properly. I call a web service to change the image on a server. The problem is even after changing the image on remote server, the image on the browser is not reflecting the change even on refreshing the page.
I tried using the meta tags to disable caching but it's not working for me.
The image changes on the browser only after I delete the browser cache. How can I solve this?
Example Flow based on Comments:
http://server_path/image_name.jpg
PROBLEM: Changed image not reflected.
If it is getting cached in the browser, you can force every request for the image to use a different URL, and so always request the server, by appending a different query string to it, say equal to the current number of milliseconds since 1970. A short custom directive is probably a good way:
app.directive('noCacheSrc', function($window) {
return {
priority: 99,
link: function(scope, element, attrs) {
attrs.$observe('noCacheSrc', function(noCacheSrc) {
noCacheSrc += '?' + (new Date()).getTime();
attrs.$set('src', noCacheSrc);
});
}
}
});
that is used as
<img no-cache-src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Swallow_flying_drinking.jpg/320px-Swallow_flying_drinking.jpg">
(much as ngSrc
would be used). You can see a demo in this plunker.
Note, the directive as written will probably not work if the src
already has a query string. It might need to test for the presence of a ?
and adjust how it behaves (i.e. if there is a ?
, add &
+ seconds instead).
Edit: simplified by just using one directive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With