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
- have you tried to do something like $templateCache.remove('template/file.html') .... i think it is a mon cache sistem and it use then path and name of a view as the key – federico scamuzzi Commented Dec 28, 2016 at 14:16
- yes i did try that, but its not working for me – Kunal arora Commented Dec 28, 2016 at 14:21
-
remove(key)
should work. – tasseKATT Commented Dec 28, 2016 at 14:23 -
@tasseKATT what to write in
key
. can you show with an example? – Kunal arora Commented Dec 28, 2016 at 14:28 - The first answer tells that $templateCache use in fact $cacheFactory Link: stackoverflow./questions/25267962/… Try this: $cacheFactory.Cache.remove('yourTemplate') Info: docs.angularjs/api/ng/type/$cacheFactory.Cache – Aliz Commented Dec 28, 2016 at 14:32
3 Answers
Reset to default 1The $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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745276474a4620072.html
评论列表(0条)