禁用AngularJS $ http缓存

时间:2016-07-05 06:21:23

标签: angularjs http caching

我试图在我的AngularJS应用中禁用缓存,但它没有使用以下代码:

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

当我使用"myurl&random="+Math.random()时,缓存被禁用;但是,我想采用不同的方法。

4 个答案:

答案 0 :(得分:32)

已经回答here

从链接中粘贴代码段以供参考。

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';
}]);

答案 1 :(得分:9)

这是我做的,只需将其改为

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}})

答案 2 :(得分:1)

试试这个

  $state(' 'app.name', {
 url: '/name',
cache: false,
 controller:'MainCtrl',
 templateUrl:'templates/name.html'
 })

答案 3 :(得分:0)

myApp.config(function ($routeProvider) {
        $routeProvider.
            when('/', {controller: 'MyCtrl', templateUrl: '/eshop/myConfig'})
})

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

我没有测试过它。我在这个网址中找到了一些东西。看看这个 Angularjs - how to clear $routeProvider's caches of templateUrl

相关问题