当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > 关于angularJs清除浏览器缓存的方法

关于angularJs清除浏览器缓存的方法

2017年12月08日  | 移动技术网IT编程  | 我要评论
缓存篇 一个缓存就是一个组件,它可以透明地储存数据,以便以后可以更快地服务于请求。多次重复地获取资源可能会导致数据重复,消耗时间。因此缓存适用于变化性不大的一些数据,缓存

缓存篇

一个缓存就是一个组件,它可以透明地储存数据,以便以后可以更快地服务于请求。多次重复地获取资源可能会导致数据重复,消耗时间。因此缓存适用于变化性不大的一些数据,缓存能够服务的请求越多,整体系统性能就能提升越多。

浏览器缓存,有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能。但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数据。像股票类网站实时更新等,这样的网站是不要缓存的,像有的网站很少更新,有缓存还是比较好的。

以下是传统的清除浏览器的方法

meta方法

//不缓存 
<meta http-equiv="pragma" content="no-cache">  
<meta http-equiv="cache-control" content="no-cache, must-revalidate">  
<meta http-equiv="expires" content="0"> 

清理form的临时缓存

<body onload="javascript:document.yourformname.reset()"> 

ajax清除缓存

$.ajax({ 
   url:'www.haorooms.com', 
   datatype:'json', 
   data:{}, 
   cache:false,  
   ifmodified :true , 
 
   success:function(response){ 
     //操作 
   } 
   async:false 
 }); 

用随机数,随机数也是避免缓存的一种很不错的方法!

url 参数后加上 "?ran=" + math.random(); //当然这里参数 ran可以任意取了 

用随机时间,和随机数一样。

在 url 参数后加上 "?timestamp=" + new date().gettime();  

用php后端清理

在服务端加 header("cache-control: no-cache, must-revalidate");等等(如php中) 

下面介绍关于angularjs项目中清除浏览器的方法,当然以上传统的方法也是可以适用的,但对于angularjs来说还需添加以下几项:

一、清除模板缓存

.run(function($rootscope, $templatecache) {  
      $rootscope.$on('$routechangestart', function(event, next, current) {  
        if (typeof(current) !== 'undefined'){  
          $templatecache.remove(current.templateurl);  
        }  
      });  
    });  

二、html添加随机参数

.state("content", { 
        url: "/", 
        views:{ 
          "bodyinfo":{templateurl: 'tpls/bodyinfo.html?'+ +new date(), 
            controller:'bodyinfoctrl'}, 
          "header":{templateurl: 'tpls/header.html?'+ +new date(), 
            controller:'headerctrl' 
          }, 
          "footer":{templateurl: 'tpls/footer.html?'+ +new date(), 
            controller:'footerctrl' 
          } 
        } 
      }) 
<link rel="stylesheet" href="stylesheets/main.css?version=1.0.3" rel="external nofollow" > 

三、清除route缓存

.config(['$stateprovider', '$urlrouterprovider','$locationprovider','$httpprovider',function($stateprovider, $urlrouterprovider,$locationprovider,$httpprovider) { 
//     $urlrouterprovider.when("", "/home"); 
      $urlrouterprovider.otherwise('/'); 
       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'; 

好了……就这么多了

如果还有其他方法欢迎指点迷津!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网