1

I have routing set up via $routeProvider, something like

$routeProvider.when('/page/:id', 
{templateUrl: 'partials/public/view/page.html', controller: XYZ});

On first page load, I'd like to iterate through cached partials and remove them. Initially I've tried:

$templateCache.removeAll()

called when parent controller (declared over entire body element) is first created - aka page load. Which seems to work, but some 3rd party plugins (directives) then throw error

Error: Failed to load template: template/tabs/tab.html

I know I can remove one partial cache at a time using $templateCache.remove(key) but I don't like the idea of hardcoding all partials keys in some custom clear function. Instead, I'd like to get all cached partials and if key starts with some string, then remove it. My question is, is there a way to iterate all cached partials?

6
  • 1
    Why do you want to remove anything from the template cache? I don't understand the use case. Commented Jan 14, 2014 at 22:11
  • Because the app is already deployed to server in sort of testing mode and I'm still pushing changes so I want to make sure that once the page is loaded, users will have up to date information. I won't care much about it when it is stabilized, but right now I do need to be sure to avoid false errors. Commented Jan 14, 2014 at 23:03
  • 1
    So... request a different file? Deleting things from cache like that is problematic to get right. Commented Jan 14, 2014 at 23:04
  • By requesting different file you mean adding e.g. URL parameter to $routeProvider's when() ? The thing I don't like about that it is a useless change in repository and I'd need some tool to do it automatically prior push (well some ant task would do the trick). But it could work. Commented Jan 14, 2014 at 23:10
  • You should have a way to version your javascript files to handle cache busting already and it should be a part of your build process (and if you don't, you should do it right away--especially before you go officially live). And I mean changing the template url itself. Commented Jan 14, 2014 at 23:34

1 Answer 1

1

I don't think there's an easy way to iterate $templateCache. FYI, I got your error when using Angular's UI Bootstrap. A short way to solve this error is to remove the new cache entry on changing the route instead of removing all entries:

...

app.run(function($rootScope, $templateCache) {
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if (typeof(current) !== 'undefined'){
            $templateCache.remove(current.templateUrl);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.