2015-03-09 12 views
5

Quando elencho l'oggetto $ cacheFactory ha diversi metodi, ma non vedo la cache valore/chiave effettiva.Ottieni chiavi da cache angolareFactory

Supponendo che si stia guardando la cache $ http, $ cacheFactory ($ http) come si può ottenere un elenco di chiavi o idealmente, chiavi e valori attualmente memorizzati nella cache?

+0

Domanda interessante, mi chiedevo la stessa cosa! – teone

risposta

2

Uso un decoratore della $cacheFactory per aggiungere un metodo getKeys:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 

 
<body ng-app="decorateExample"> 
 

 
    <script> 
 
    function cacheProvider($provide) 
 
    { 
 
    // monkey-patches $templateCache to have a keys() method 
 
    $provide.decorator('$templateCache', ['$delegate', cacheDelegator]); 
 
    } 
 
              
 
    function cacheDelegator($delegate) 
 
    { 
 
    var keys = [], origPut = $delegate.put; 
 

 
    $delegate.put = function(key, value) 
 
     { 
 
     origPut(key, value); 
 
     keys.push(key); 
 
     }; 
 

 
    // we would need cache.peek() to get all keys from $templateCache, but this features was never 
 
    // integrated into Angular: https://github.com/angular/angular.js/pull/3760 
 
    // please note: this is not feature complete, removing templates is NOT considered 
 
    $delegate.getKeys = function() 
 
     { 
 
     return keys; 
 
     }; 
 

 
    return $delegate; 
 
    } 
 
    
 
angular.module('decorateExample', []); 
 
    
 
angular.module('decorateExample').config(['$provide', cacheProvider]); 
 
</script> 
 

 
</body>

Riferimenti

+0

Questo è un ottimo suggerimento, ma sfortunatamente funziona solo con '$ templateCache' e non può essere applicato ad altre cache come' $ http'. Se si desidera modificare l'oggetto cache che utilizza il servizio $ http, è necessario crearne uno e modificarlo manualmente con i metodi necessari. – n1313

Problemi correlati