Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to manually clear out $templateCache in AngularJS

I'm working on a system that has a lot of html template files being used all throughout the system.

The problem is when I deploy updates, users are unable to see html changes because their browsers are using the cached version of them. So far, the only way I am able to get users to see html updates is to have them perform a hard reload of their browser.

For obvious reasons this is not ideal. As mre users migrate to this system, it has become a tedious chore making sure everyone performs that action

Is there a way I can force browsers accessing the system to clear template cache at my command? Maybe manually clear it upon logging into the system?

like image 652
mrb398 Avatar asked Jul 21 '15 12:07

mrb398


People also ask

How do I clear a template cache?

First import the TemplateCompiler. import { TemplateCompiler } from 'angular2/src/compiler/template_compiler'; Next inject the TemplateCompiler in your constructor. Finally use that to clear the cache.

What is$ templateCache?

$templateCache is a Cache object created by the $cacheFactory. The first time a template is used, it is loaded in the template cache for quick retrieval. You can load templates directly into the cache in a script tag, by using $templateRequest , or by consuming the $templateCache service directly.


2 Answers

$templateCache.removeAll() is good to remove template cache

example code

myApp.run(function($rootScope, $templateCache) {
   $rootScope.$on('$viewContentLoaded', function() {
      $templateCache.removeAll();
   });
});
like image 161
Ramesh Rajendran Avatar answered Oct 14 '22 09:10

Ramesh Rajendran


You can use angular-cache-buster plugin. I have also had the problem of loading the cached templates and the issue was solved by using this great plugin.

https://github.com/saintmac/angular-cache-buster

like image 36
Sajan Mullappally Avatar answered Oct 14 '22 09:10

Sajan Mullappally