我在AngularJS中缓存部分有问题。
在我的HTML页面,我有:
<body>
<div ng-view></div>
<body>
我的偏导数被载入的地方。
当我改变HTML代码在我的部分,浏览器仍然加载旧数据。
有什么解决办法吗?
我在AngularJS中缓存部分有问题。
在我的HTML页面,我有:
<body>
<div ng-view></div>
<body>
我的偏导数被载入的地方。
当我改变HTML代码在我的部分,浏览器仍然加载旧数据。
有什么解决办法吗?
当前回答
在@Valentyn回答的基础上,这里有一种方法可以在ng-view内容发生变化时自动清除缓存:
myApp.run(function($rootScope, $templateCache) {
$rootScope.$on('$viewContentLoaded', function() {
$templateCache.removeAll();
});
});
其他回答
我发布这篇文章只是为了涵盖所有的可能性,因为其他的解决方案都不适合我(他们由于角引导模板依赖而抛出错误)。
当你在开发/调试一个特定的模板时,你可以确保它总是通过在路径中包含一个时间戳来刷新,就像这样:
$modal.open({
// TODO: Only while dev/debug. Remove later.
templateUrl: 'core/admin/organizations/modal-selector/modal-selector.html?nd=' + Date.now(),
controller : function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.close();
};
}
});
注意templateUrl变量中最后的?nd=' + Date.now()。
正如在其他答案中提到的,这里和这里,缓存可以通过使用:
$templateCache.removeAll();
然而,正如gateatigrado在评论中所建议的那样,这只在html模板没有任何缓存头的情况下才会起作用。
这对我来说是可行的:
角:
app.run(['$templateCache', function ( $templateCache ) {
$templateCache.removeAll(); }]);
您可能会以各种方式添加缓存头,但这里有几个解决方案适合我。
如果使用IIS,请将此添加到web.config中:
<location path="scripts/app/views">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
如果使用Nginx,你可以添加到你的配置:
location ^~ /scripts/app/views/ {
expires -1;
}
Edit
我刚刚意识到这个问题提到了开发机器,但希望这仍然可以帮助到一些人……
这是Chrome的另一个选项。
按F12打开开发人员工具。然后资源>缓存存储>刷新缓存。
我喜欢这个选项,因为我不需要像其他答案那样禁用缓存。
没有解决方案可以防止浏览器/代理缓存,因为您无法控制它。
另一种方式,强制新鲜内容给您的用户它重命名HTML文件!就像https://www.npmjs.com/package/grunt-filerev对资产所做的那样。
这个代码片段帮助我摆脱了模板缓存
app.run(function($rootScope, $templateCache) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if (typeof(current) !== 'undefined'){
$templateCache.remove(current.templateUrl);
}
});
});
以下片段的详细信息可在此链接中找到: http://oncodesign.io/2014/02/19/safely-prevent-template-caching-in-angularjs/