4

I know how to remove all templates from cache by

$templateCache.removeAll();

But I want to remove a specific template only from cache. The template is not loaded from $routeProvider but it is being rendered from a directive by using

templateUrl: 'template/page/file.html'

My app structure is as this

-web
  - js
    - controllers
       - appController.js
  - templates
    - page
      - file.html

I did $templateCache.remove('/templates/page/file.html'); in appController.js

Kunal arora
  • 167
  • 1
  • 2
  • 8
  • have you tried to do something like $templateCache.remove('template/file.html') .... i think it is a common cache sistem and it use then path and name of a view as the key – federico scamuzzi Dec 28 '16 at 14:16
  • yes i did try that, but its not working for me – Kunal arora Dec 28 '16 at 14:21
  • `remove(key)` should work. – tasseKATT Dec 28 '16 at 14:23
  • @tasseKATT what to write in `key`. can you show with an example? – Kunal arora Dec 28 '16 at 14:28
  • The first answer tells that $templateCache use in fact $cacheFactory Link: http://stackoverflow.com/questions/25267962/remove-template-cache-on-logout-angular-js Try this: $cacheFactory.Cache.remove('yourTemplate') Info: https://docs.angularjs.org/api/ng/type/$cacheFactory.Cache – Aliz Dec 28 '16 at 14:32

3 Answers3

1

Try to do this in your run method of the app :

 app.run([
        "$rootScope", "$templateCache", "authService", function($rootScope, $templateCache, authService) {
            $rootScope.$on("$routeChangeStart", function(event, next, current) {

                    if (typeof (current) !== "undefined") {
                        $templateCache.remove(current.templateUrl);
                    }


            });
        }
    ]);
federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24
1

The $templateCache is based off the $cacheFactory and since the latter has a .remove(key), it will work on your $templateCache as well. Perhaps you got the wrong key?

You could try and call .get(key) to check if you do have the correct key (i suspect you don't - and that is the reason .remove(key) doesn't work for you).

Perhaps the path to your template file is messing up the key, a relative path might not be the actual key, but rather the full path or the filename alone.

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
1

Problem was with my code, the function trigger was not right. I solved it by

$rootScope.$on('$viewContentLoaded', function() {
    $templateCache.remove('templates/page/file.html');
});

so now the template is removed from the cache when the page is loaded. Thanks for all the help.

Kunal arora
  • 167
  • 1
  • 2
  • 8