2015-12-06 27 views
1

我所做的所有http请求都默认缓存。现在,我如何清除特定请求的缓存?angularjs从特定请求中清除缓存

这里是一个更好的解释情况。我有一个REST API根据身份验证发送两种不同类型的数据。每当用户进行身份验证时,我需要清除旧数据并通过身份验证发出新请求。我的控制器被刷新,但是由于调用已经被缓存,因此没有进行API调用。它返回旧数据。

+0

您如何设置缓存?通过将'$ http Cache'属性设置为true? – sjokkogutten

回答

0

您可以使用我们的自定义缓存对象角的内置$cacheFactory

E.g

// cache the HTTP response 
$http.get('myurl',{ 
    cache: true 
} 

// this object can now be retrieved using $cacheFactory 
var httpCache = $cacheFactory('$http'); 

// to remove the value from the cache, get the default $http cache, call the remove function and pass in the url 
var httpCache = $cacheFactory.get('$http'); 
httpCache.remove('myurl'); 
0

告诉我们的$ HTTP请求,通过我们自己的自定义缓存提出要求很简单。我们可以传递缓存的实例,而不是在请求中传递布尔值true。

var myCache = $cacheFactory.get('myCache'); 
$http({ 
    method: 'GET', 
    url: '/api/users.json', 
    cache: myCache 
}); 
// Or, using the .get helper 
$http.get('/api/users.json', { 
    cache: myCache 
}); 

现在,$ http将使用我们的自定义缓存,而不是使用默认缓存。