我在使用AngularJS缓存部分内容时遇到了问题。
我的HTML页面中有以下代码:
<body>
<div ng-view></div>
<body>
我的部分视图文件在哪里加载。
当我更改部分视图文件中的HTML代码时,浏览器仍会加载旧数据。
有什么解决方法吗?
我在使用AngularJS缓存部分内容时遇到了问题。
我的HTML页面中有以下代码:
<body>
<div ng-view></div>
<body>
我的部分视图文件在哪里加载。
当我更改部分视图文件中的HTML代码时,浏览器仍会加载旧数据。
有什么解决方法吗?
对于开发,您也可以禁用浏览器缓存 - 在Chrome Dev Tools中,在右下角点击齿轮并选中选项
在DevTools打开时禁用缓存
更新:在Firefox中,调试器->设置->高级部分中也有相同的选项(适用于版本33)。
更新2:尽管此选项出现在Firefox中,但有些人报告说它不起作用。建议使用Firebug并按照hadaytullah的答案操作。
myApp.run(function($rootScope, $templateCache) {
$rootScope.$on('$viewContentLoaded', function() {
$templateCache.removeAll();
});
});
正如其他答案中所提到的,这里和这里,可以通过使用以下方式清除缓存:
$templateCache.removeAll();
然而,如gatoatigrado在评论中所建议的,这只有在HTML模板没有使用任何缓存头部信息时才能起作用。
因此,以下方法适用于我:
在Angular中:
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;
}
编辑
我刚刚意识到问题提到了dev
机器,但希望这仍然能帮助某人...
如果您正在谈论用于缓存模板而不需要重新加载整个页面的缓存,那么您可以通过类似以下方式来清空它:
.controller('mainCtrl', function($scope, $templateCache) {
$scope.clearCache = function() {
$templateCache.removeAll();
}
});
在标记中:
<button ng-click='clearCache()'>Clear cache</button>
按下此按钮以清除缓存。
这个片段帮助我摆脱了模板缓存
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/
由于其他解决方案均无法解决我的问题(由于 angular-bootstrap 模板依赖等原因造成错误),因此我在此发布帖子以涵盖所有可能性。
在您开发/调试特定模板时,可以通过在路径中包含时间戳来确保其始终刷新,像这样:
$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()
。
.value('DEBUG', true)
来启用或禁用该行。 - Diosney.run(function($rootScope) { $rootScope.DEBUG = true; ...
,然后在指令中注入$rootScope,如下所示:.directive('filter', ['$rootScope', function($rootScope)...
,并在返回的对象属性中添加:templateUrl: '/app/components/filter/filter-template.html' + ($rootScope.DEBUG ? '?n=' + Date.now() : '')
。也许您可以详细说明一下您的.value('DEBUG', true)方法?已点赞! - JackLeEmmerdeur.value('DEBUG', true)
的方式与 $rootScope
相同,但不会使其混乱 :) 你可以稍后将 DEBUG
注入到控制器中,并像普通服务一样查询。 - Diosney.value(...)
这个东西,如果不太复杂的话?我猜背后的概念是我不知道的Angular最佳实践。 - JackLeEmmerdeur正如其他人所说,为了开发目的完全禁用缓存可以很容易地在不更改代码的情况下实现:使用浏览器设置或插件。在开发之外,要打败基于路由的Angular模板缓存,请在$ routeChangeStart(或对于UI Router的$ stateChangeStart)期间从缓存中删除模板URL,如Shayan所示。但是,这并不影响通过ng-include加载的模板的缓存,因为这些模板不通过路由加载。
我想能够快速修复生产中包括通过ng-include加载的任何模板,并使用户能够快速在其浏览器中接收到热修复,而无需重新加载整个页面。我也不关心针对模板的HTTP缓存。解决方案是拦截应用程序发出的每个HTTP请求,忽略那些不是用于我的应用程序的 .html 模板的请求,然后向模板的URL添加一个每分钟更改一次的参数。请注意,路径检查特定于您的应用程序模板的路径。要获得不同的间隔,请更改参数数学,或完全删除%以获取没有缓存的结果。
// this defeats Angular's $templateCache on a 1-minute interval
// as a side-effect it also defeats HTTP (browser) caching
angular.module('myApp').config(function($httpProvider, ...) {
$httpProvider.interceptors.push(function() {
return {
'request': function(config) {
config.url = getTimeVersionedUrl(config.url);
return config;
}
};
});
function getTimeVersionedUrl(url) {
// only do for html templates of this app
// NOTE: the path to test for is app dependent!
if (!url || url.indexOf('a/app/') < 0 || url.indexOf('.html') < 0) return url;
// create a URL param that changes every minute
// and add it intelligently to the template's previous url
var param = 'v=' + ~~(Date.now() / 60000) % 10000; // 4 unique digits every minute
if (url.indexOf('?') > 0) {
if (url.indexOf('v=') > 0) return url.replace(/v=[0-9](4)/, param);
return url + '&' + param;
}
return url + '?' + param;
}
function configureTemplateFactory($provide) {
// Set a suffix outside the decorator function
var cacheBust = Date.now().toString();
function templateFactoryDecorator($delegate) {
var fromUrl = angular.bind($delegate, $delegate.fromUrl);
$delegate.fromUrl = function (url, params) {
if (url !== null && angular.isDefined(url) && angular.isString(url)) {
url += (url.indexOf("?") === -1 ? "?" : "&");
url += "v=" + cacheBust;
}
return fromUrl(url, params);
};
return $delegate;
}
$provide.decorator('$templateFactory', ['$delegate', templateFactoryDecorator]);
}
app.config(['$provide', configureTemplateFactory]);
我相信您可以通过在$routeProvider中装饰“when”方法来实现相同的结果。
Date
的dev cachebusting方法的代码样式。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');
}]);
flask_app.py
中添加了app.config.update(SEND_FILE_MAX_AGE_DEFAULT=0)
。(我想其他网络服务器也存在类似的情况)。 - gatoatigradong-include
|ng-view
|templateUrl
的ajax请求不会被这个快捷方式处理。 - André Werlang