我在AngularJS中缓存部分有问题。
在我的HTML页面,我有:
<body>
<div ng-view></div>
<body>
我的偏导数被载入的地方。
当我改变HTML代码在我的部分,浏览器仍然加载旧数据。
有什么解决办法吗?
我在AngularJS中缓存部分有问题。
在我的HTML页面,我有:
<body>
<div ng-view></div>
<body>
我的偏导数被载入的地方。
当我改变HTML代码在我的部分,浏览器仍然加载旧数据。
有什么解决办法吗?
当前回答
我发布这篇文章只是为了涵盖所有的可能性,因为其他的解决方案都不适合我(他们由于角引导模板依赖而抛出错误)。
当你在开发/调试一个特定的模板时,你可以确保它总是通过在路径中包含一个时间戳来刷新,就像这样:
$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()。
其他回答
这个代码片段帮助我摆脱了模板缓存
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/
Firefox(33.1.1)使用Firebug(22.0.6)的解决方案
工具> Web-Tools > Firebug > Firebug。 在Firebug视图中转到“Net”视图。 一个下拉菜单符号将出现在“Net”(视图的标题)旁边。 从下拉菜单中选择“禁用浏览器缓存”。
我发现HTTP拦截器方法工作得非常好,并允许额外的灵活性和控制。此外,您可以为每个产品版本使用一个发布散列作为buster变量来缓存bust。
下面是使用Date的dev缓存破坏方法。
app.factory('cachebustInjector', function(conf) {
var cachebustInjector = {
request: function(config) {
// new timestamp will be appended to each new partial .html request to prevent caching in a dev environment
var buster = new Date().getTime();
if (config.url.indexOf('static/angular_templates') > -1) {
config.url += ['?v=', buster].join('');
}
return config;
}
};
return cachebustInjector;
});
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('cachebustInjector');
}]);
我发布这篇文章只是为了涵盖所有的可能性,因为其他的解决方案都不适合我(他们由于角引导模板依赖而抛出错误)。
当你在开发/调试一个特定的模板时,你可以确保它总是通过在路径中包含一个时间戳来刷新,就像这样:
$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()。
对于开发,你也可以停用浏览器缓存-在Chrome开发工具右下角点击齿轮,并勾选选项
禁用缓存(当DevTools打开时)
更新:在Firefox中有相同的选项调试器->设置->高级部分(选中版本33)
更新2:虽然这个选项出现在Firefox一些报告,它不工作。我建议使用firebug并遵循hadaytullah的回答。