Angularjs $http缓存刷新

标签 angularjs http caching

我需要在向数据库添加新数据后刷新缓存。

angular.module('app').factory('myService', function ($http,$cacheFactory) {

  var profileCache=$cacheFactory('profiles');

  //get list of items
  function getItem(){
   var request=$http({
     method:'get',
     url:domain+'/api/v1/items/list',
     cache:profileCache,
     params:{
       action:'get'
     }
   });
   return(request.then(response.data));
 }
// Post items
 function addItem(item){
   var request=$http({
     method:'post',
     url:domain+'/api/v1/items/add',
     data:item
   });
   return(request.then(response.data));
 }
})

我想在添加项目后刷新缓存,以便缓存可以有新的更改数据。

最佳答案

如你所见

https://docs.angularjs.org/api/ng/service/$http

$http 响应默认不缓存

但是如果你有特定的环境并且你需要阻止任何缓存你可以在你的请求中添加一个日期时间 例子

function getItem(){
   var d = new Date();
   var n = d.getDate();
   var request=$http({
     method:'get',
     url:domain+'/api/v1/items/list?' + n,
     cache:true,
     params:{
       action:'get'
     }
   });
   return(request.then(response.data));
 }

关于Angularjs $http缓存刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046670/

相关文章:

c# - 将 cefsharp 浏览器与 nancy 托管的 Angular 网络应用程序连接时遇到问题

http - 配置 Nginx 和 Socket.IO

javascript - 对 jquery 中的 303 状态代码使用react(防止重定向)

android - 改造 OKHTTP 离线缓存不起作用

caching - 大多数 A 级浏览器是否通过 https 将内容缓存到磁盘?

javascript - 为什么没有填充 ngRepeat

javascript - 如何使用xpath从用 Angular 编写的网页中提取文本?

angularjs - 使用 AngularJS 上传文件

python - 在 Python 中使用带有授权 HTTP header 的 GET 和 POST

http - Go Web 服务器不处理 POST 请求