SoFunction
Updated on 2025-04-13

About angularJs to clear browser cache

Cache

A cache is a component that can store data transparently so that it can serve requests faster in the future. Retrieving resources repeatedly may cause data duplication and consume time. Therefore, cache is suitable for some data that is not very variable. The more requests the cache can serve, the more the overall system performance can be improved.

Browser cache, sometimes we need it because it can improve website performance and browser speed and improve website performance. But sometimes we have to clear the cache, because the cache may make mistakes and some wrong data will appear. For real-time updates of stock websites, such websites should not be cached. Some websites rarely update, so it is better to have caches.

Here is a traditional way to clear the browser

meta method

//No cache<META HTTP-EQUIV="pragma" CONTENT="no-cache">  
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">  
<META HTTP-EQUIV="expires" CONTENT="0"> 

Clean up the temporary cache of form

<body onLoad="javascript:()"> 

ajax clears cache

$.ajax({ 
   url:'', 
   dataType:'json', 
   data:{}, 
   cache:false,  
   ifModified :true , 
 
   success:function(response){ 
     //operate   } 
   async:false 
 }); 

Using random numbers, random numbers are also a very good way to avoid cache!

Add "?ran=" + (); //Of course, the parameter ran can be taken arbitrarily.

Use random time, the same as random numbers.

Add "?timestamp=" + new Date().getTime() after the URL parameter;

Clean up with php backend

Add header("Cache-Control: no-cache, must-revalidate"); etc. on the server (such as in php)

The following is a method of clearing the browser in the angularJs project. Of course, the above traditional methods can also be applied, but for angularJs, the following items need to be added:

1. Clear template cache

.run(function($rootScope, $templateCache) {  
      $rootScope.$on('$routeChangeStart', function(event, next, current) {  
        if (typeof(current) !== 'undefined'){  
          $();  
        }  
      });  
    });  

2. Add random parameters in html

.state("content", { 
        url: "/", 
        views:{ 
          "bodyInfo":{templateUrl: 'tpls/?'+ +new Date(), 
            controller:'bodyInfoCtrl'}, 
          "header":{templateUrl: 'tpls/?'+ +new Date(), 
            controller:'headerCtrl' 
          }, 
          "footer":{templateUrl: 'tpls/?'+ +new Date(), 
            controller:'footerCtrl' 
          } 
        } 
      }) 
<link rel="stylesheet" href="stylesheets/?version=1.0.3" rel="external nofollow" > 

3. Clear route cache

.config(['$stateProvider', '$urlRouterProvider','$locationProvider','$httpProvider',function($stateProvider, $urlRouterProvider,$locationProvider,$httpProvider) { 
//     $("", "/home"); 
      $('/'); 
       if (!$) { 
       $ = {}; 
      } 
      $["X-Requested-With"] = 'XMLHttpRequest'; 
      $['Cache-Control'] = 'no-cache'; 
      $['Pragma'] = 'no-cache'; 

OK... that's all

If there are other ways, please give me some advice!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.