2015-12-06 28 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將使用我們的自定義緩存,而不是使用默認緩存。