【问题标题】:Disabling AngularJS $http cache禁用 AngularJS $http 缓存
【发布时间】:2016-11-06 21:05:13
【问题描述】:

我正在尝试禁用我的 AngularJS 应用程序中的缓存,但它不适用于以下代码:

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

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

【问题讨论】:

标签: angularjs http caching


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    myApp.config(function ($routeProvider) {
            $routeProvider.
                when('/', {controller: 'MyCtrl', templateUrl: '/eshop/myConfig'})
    })
    
    .controller('MyCtrl', function ($scope, $templateCache) {
        $templateCache.remove('/eshop/myConfig');
        // or
        $templateCache.removeAll();
    });
    

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

    【讨论】:

      【解决方案3】:

      这已经回答here

      从链接中粘贴代码 sn-p 供您参考。

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

      【讨论】:

      • 我建议不要使用 Pragma one。这不是标准(显然),可能会导致跨域请求出现问题。刚刚与朋友完成了 2 小时的调试会话,以了解问题的原因。
      • @guymograbi 那你有什么推荐的?
      • @Schoof 如果您只是删除 Pragma,问题是否仍然存在?
      • 在chrome后退按钮上我用来获取相同URL的json bcoz,从上面的代码中我解决了谢谢!!
      【解决方案4】:

      这样试试

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

      【讨论】:

      • 不确定这个答案如何应用。问题是关于http get。不是模板。
      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 2014-11-06
      • 2016-08-31
      • 2019-02-23
      • 1970-01-01
      • 2015-12-29
      • 2016-09-30
      • 2015-12-22
      相关资源
      最近更新 更多