角 - 在服务器改变图像鉴于没有反映 [英] Angular - Changed image in server not reflecting in view
问题描述
我有一个角度应用程序,它向人们展示个人资料图片(从服务器下载),并有一个选项来改变它。
I have an Angular application which shows people profile images(loaded from a server) and has an option to change it.
当应用程序加载在第一时间,正确显示图像。我调用Web服务更改服务器上的图像。问题是,即使改变远程服务器上的图像之后,在浏览器上的图像不反射即使在刷新页面的变化。
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.
我尝试使用meta标签禁用缓存,但它不是为我工作。
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?
根据实例点评流量:
- 初始路径:
HTTP://server_path/image_name.jpg
- 调用Web服务,改变形象(注意:图片的路径不会改变,只有
图像本身被替换) - 刷新浏览器的页面
问题:改变形象没有反映
PROBLEM: Changed image not reflected.
推荐答案
如果它在浏览器中得到缓存,可以强制每个请求的图像使用不同的URL,因此总是请求服务器,通过附加不同的查询字符串来呢,说从1970年开始等于毫秒目前一些短定制指令可能是一个很好的方法:
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);
});
}
}
});
这被用作
<img no-cache-src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Swallow_flying_drinking.jpg/320px-Swallow_flying_drinking.jpg">
(就像 ngSrc
将被使用)。你可以看到在这个plunker 一个演示。
(much as ngSrc
would be used). You can see a demo in this plunker.
请注意,该指令所写可能不会如的src
已经有一个查询字符串工作。它可能需要测试的?
的presence并调整它的行为(即,如果有一个?
添加&安培;
+秒代替)
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).
编辑:只需使用一个简化的指令
simplified by just using one directive.
这篇关于角 - 在服务器改变图像鉴于没有反映的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!