如何在 AngularJS 中刷新/使 $resource 缓存失效 [英] How to refresh / invalidate $resource cache in AngularJS

查看:26
本文介绍了如何在 AngularJS 中刷新/使 $resource 缓存失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 User $resource,它使用默认的 $http 缓存实现,如下所示:

I have a simple User $resource that uses the default $http cache implementation like so:

factory('User', function($resource){
    return $resource(endpoint + '/user/current/:projectId', {},
        {get: 
            {
                cache: true,
                method: 'GET'
            }
        }
    );
})

这很有效,即我的服务器在我的应用程序中只调用一次,然后从缓存中获取值.

This works very well, i.e. my server is only called once in my application, then the value is fetched from cache.

但是我需要在某个操作后从服务器刷新该值.有没有简单的方法可以做到这一点?

But I need to refresh the value from the server after a certain operation. Is there an easy way to do that?

谢谢.

推荐答案

保留布尔值并获取 $http 缓存:

Keep the boolean and get the $http cache:

var $httpDefaultCache = $cacheFactory.get('$http');

然后您可以像使用 $cacheFactory 制作的任何其他缓存一样控制它,下面提供了一个用法实例:

Then you can control it like any another cache made with $cacheFactory, a usage instance provided below:

$httpDefaultCache.remove(key);
// Where key is the relative URL of your resource (eg: /api/user/current/51a9020d91799f1e9b8db12f)

这篇关于如何在 AngularJS 中刷新/使 $resource 缓存失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆