问题描述
一旦用户单击注销,如何删除 Angular 模板缓存?我们进行了彻底的研究,并尝试实施大多数可用的解决方案.我们尝试了以下
How to remove Angular template cache once user clicks on logout? We did thorough research and tried to implement most of the solutions available out there. we tried following
HTML 元标记
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
服务器端
res.setHeader('cache-control', 'no-cache', 'no-store', 'must-revalidate');
Javascript
我们尝试删除此博客中指定的浏览器历史记录一个>
我们现在面临的问题是当用户从我们的应用程序注销并按下后退按钮时,他会看到空的 html 模板,因为服务没有被缓存,我们应该如何确保模板也没有被缓存?
Problem we are facing right now is when a user logs out from our app and press back button he is shown empty html templates as services are not cached how shall we make sure templates are not cached either?
推荐答案
$templateCache
只是具有特定名称的 $cacheFactory
的包装器.这实际上是它的所有代码:
$templateCache
is just a wrapper around $cacheFactory
with a specific name. This is literally all of its code:
function $TemplateCacheProvider() {
this.$get = ['$cacheFactory', function($cacheFactory) {
return $cacheFactory('templates');
}];
}
$cacheFactory
有一个 removeAll()
方法.试试这个,如果你想自己连接它(例如在你注入 $cacheFactory
的服务中):
$cacheFactory
has a removeAll()
method. Try this, if you want to wire it up yourself (e.g. in a service into which you've injected $cacheFactory
):
$cacheFactory('templates').removeAll();
但另外,如果您的应用程序对安全敏感,也许您想完全禁用缓存.您可以在整个浏览器上执行 reload() 调用以转储所有本地 JS 变量,然后您上面针对实际 HTTP 驱动资产的 no-cache pragma 将执行您想要的操作.
But separately, maybe you want to disable caching entirely if your application is security-sensitive. You can do a reload() call on the entire browser to dump all local JS variables, and then your no-cache pragmas above on the actual HTTP-driven assets will do what you want.
这篇关于在注销 Angular.js 时删除模板缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!