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
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
remove(key)
should work.
– tasseKATT
Commented
Dec 28, 2016 at 14:23
key
. can you show with an example?
– Kunal arora
Commented
Dec 28, 2016 at 14:28
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.
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);
}
});
}
]);
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.