Disabling AngularJS $http cache

2019-01-24 02:50发布

I'm trying to disable the cache in my AngularJS app, but it isn't working with the following code:

$http.get("myurl",{cache:false})

When I use "myurl&random="+Math.random(), the cache is disabled; but, I'd like a different approach.

4条回答
Explosion°爆炸
2楼-- · 2019-01-24 03:43

Here is what I did, simply change it to

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}})
查看更多
仙女界的扛把子
3楼-- · 2019-01-24 03:43
myApp.config(function ($routeProvider) {
        $routeProvider.
            when('/', {controller: 'MyCtrl', templateUrl: '/eshop/myConfig'})
})

.controller('MyCtrl', function ($scope, $templateCache) {
    $templateCache.remove('/eshop/myConfig');
    // or
    $templateCache.removeAll();
});

i didn't test it.i found something in this url.Have a look at this Angularjs - how to clear $routeProvider's caches of templateUrl

查看更多
不美不萌又怎样
4楼-- · 2019-01-24 03:46

try like this

  $state(' 'app.name', {
 url: '/name',
cache: false,
 controller:'MainCtrl',
 templateUrl:'templates/name.html'
 })
查看更多
混吃等死
5楼-- · 2019-01-24 03:50

This is already answered here.

Pasting code snippet from the link for your reference.

myModule.config(['$httpProvider', function($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }    

    // Answer edited to include suggestions from comments
    // because previous version of code introduced browser-related errors

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
    // extra
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
查看更多
登录 后发表回答