Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I list everything in the template cache?

Tags:

angularjs

I'm having trouble figuring out what's in my template cache. When I google, all I find is this: https://docs.angularjs.org/api/ng/service/$templateCache I'm looking for documentation on all the methods available for that but can't find it. But really all I want to know is how to list all the keys in the templateCache. How is that done?

like image 324
Daniel Kaplan Avatar asked Jul 09 '15 19:07

Daniel Kaplan


1 Answers

I think you can use $provide.decorator to wrap the put method and keep a list of the keys in the cache as they are added. You can then use the same idea to also add a new method to the cache factory that returns said list.

You can see an example implementation here, should be quite easy to adapt it to your needs: https://github.com/angular/angular.js/pull/3760#issuecomment-130343142 (pasted here for reference).

app.config(['$provide', function($provide) {

    // monkey-patches $templateCache to have a keys() method
    $provide.decorator('$templateCache', [
        '$delegate', function($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;
        }
    ]);
}]);
like image 82
fabio.sussetto Avatar answered Oct 28 '22 15:10

fabio.sussetto