如题。angularjs加载不同的路由模板,但是它总是使用缓存,重新加载页面都没用。这样岂不是都取不到数据库的最新数据。如何才能禁止路由机制使用缓存?比如:我首次进入修改界面,修改完数据保存,然后返回在进入编辑页面 ,页面内容是修改之前的内容,但是数据库中这些数据已经被更改过了!angularJS 总是会读取route缓存中的内容,而不是去读取数据库了,这种解决掉这种缓存

解决方案 »

  1.   

    var app = angular.module('phonecat', ['ngRoute']);
    app.config(['$routeProvider', '$httpProvider',
      function($routeProvider, $httpProvider) {
        if (!$httpProvider.defaults.headers.get) {
          $httpProvider.defaults.headers.get = {};
        }
        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
        $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
        $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';    $routeProvider.
        when('/phones', {
          templateUrl: 'phone-list.html',
          controller: PhoneListCtrl
        }).
        when('/phones/:phoneId', {
          templateUrl: 'phone-detail.html',
          controller: PhoneDetailCtrl
        }).
        otherwise({
          redirectTo: '/phones'
        });
      }
    ]);
      

  2.   

                  .factory('myInterceptor', function ($q) {
                      var interceptor = {
                          'request': function (config) {
                              // 成功的请求方法
                              var ts = '?^=' + new Date().getTime();
                              if (config.url.indexOf('?') > 0)
                                  config.url = config.url.replace('?', ts + '&');
                              else
                                  config.url = config.url + ts;
                          
                              return config; // 或者 $q.when(config);
                          },
                          'response': function (response) {
                              // 响应成功
                              return response; // 或者 $q.when(config);
                          },
                          'requestError': function (rejection) {
                              // 请求发生了错误,如果能从错误中恢复,可以返回一个新的请求或promise
                              return response; // 或新的promise
                              // 或者,可以通过返回一个rejection来阻止下一步
                              // return $q.reject(rejection);
                          },
                          'responseError': function (rejection) {
                              // 请求发生了错误,如果能从错误中恢复,可以返回一个新的响应或promise
                              return rejection; // 或新的promise
                              // 或者,可以通过返回一个rejection来阻止下一步
                              // return $q.reject(rejection);
                          }
                      };
                      return interceptor;
                  })
                .config(['$httpProvider', function ($httpProvider) {
                    $httpProvider.interceptors.push('myInterceptor');
                }])Powered By:http://www.jinwmmail.xyz:8080/
      

  3.   

    请求数据是不是用$http.get了,第二个参数那里,加个cache:false试试
      

  4.   

    二楼的方法,连html模板都缓存不了了