Angularjs实现页面模板清除的方法
前几天项目在上线过程中,出现了一些新问题。页面在切换时由于前一个页面的模板清理不及时,会造成页面的重叠。导致这个问题的原因是:页面模板缓存,即上一个页面退出时,浏览器没有及时清空上一个页面的模板,导致新页面加载时,旧页面模板依然存在,从而页面出现重叠。
模板缓存清除:
模板缓存的清除包括传统的 HTML标签设置清除缓存,以及angularJs的一些配置清除,和angularJs的路由切换清除
1、以下是传统的清除浏览器的方法
HTMLmeta标签设置清除缓存
<!-- 清除缓存 --> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" />
清理form表单临时缓存
<body onLoad="javascript:document.formName.reset()">
2、angularJs配置清除缓存
1、清除路由缓存,在route路由配置中,注入$httpProvider服务,通过$httpProvider服务配置,清除路由缓存。
app.config(["$stateProvider","$urlRouterProvider",'$locationProvider','$httpProvider',function ($stateProvider, $urlRouterProvider,$locationProvider,$httpProvider) {
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.get = {};
}
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
2、用随机数,随机数也是一种很不错避免缓存的的方法,即在链接 URL 参数后加上随机数(一般加时间戳) 。用随机时间,和随机数一样。
3、在状态路由配置中,将cache配置项,配置为false。
.state("discountCoupon", {
url: "/discountCoupon",
templateUrl: "discountCoupon.html?" + new Date().getTime(), //随机数
controller: 'discountCoupon',
cache: false, //cache配置
})
.state("customerPhone", {
url: "/customerPhone",
templateUrl: "customerPhone.html?" + new Date().getTime(), //随机数
controller: 'customerPhone',
cache: false, //cache配置
})
3、angularJs的路由切换清除缓存
angularJs默认 模板加载都会被缓存起来,使用的缓存服务是 $tempalteCache, 发送模板请求的服务是$templateRequest,所以可以在路由切换时将上一个页面的模板清除:
1.每次发送 $http 请求模板完成后,可以调用 $tempalteCache.remove(url) 或 $tempalteCache. removeAll 清除所有模板缓存。
$rootScope.$on('$stateChangeStart', //路由开始切换
function (event, toState, toParams, fromState, fromParams) {
//路由开始切换,清除以前所有模板缓存
if (fromState.templateUrl !== undefined) {
$templateCache.remove(fromState.templateUrl);
// $templateCache.removeAll();
}
});
$rootScope.$on('$stateChangeSuccess', //路由切换完成
function (event, toState, toParams, fromState, fromParams) {
//路由切换成功,清除上一个页面模板缓存
if (fromState.templateUrl !== undefined) {
$templateCache.remove(fromState.templateUrl);
// $templateCache.removeAll();
}
});
2.使用 $provide.decorator 改写原生的 $templateRequest (angularJs 自带 $provide服务里 $templateRequest: $TemplateRequestProvider)服务。在 $TemplateRequestProvider 服务里面我们可以看到默认使用了 $tempalteCache (本质还是 angularJs 的 $cacheFactory 服务) 服务,
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
function handleRequestFn(tpl, ignoreRequestError) {
handleRequestFn.totalPendingRequests++;
并在获取模板时,默认以 $templateCache 作为 cache使用,将获取到的模板数据,添加到 $templateCache内保存。
return $http.get(tpl, extend({
cache: $templateCache,
transformResponse: transformResponse
}, httpOptions))
['finally'](function () {
handleRequestFn.totalPendingRequests--;
})
.then(function (response) {
$templateCache.put(tpl, response.data);
return response.data;
}, handleError);
所以可以通过禁掉缓存,在 $templateRequest 的源码中将 $tempalteCache去掉,达到清除模板缓存的目的,不过这个一般不建议直接修改框架源代码!
总结
以上所述是小编给大家介绍的Angularjs实现页面模板清除的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
详解Angular之constructor和ngOnInit差异及适用场景
这篇文章主要介绍了详解Angular之constructor和ngOnInit差异及适用场景的相关资料,有兴趣的可以了解一下2017-06-06
angularjs在ng-repeat中使用ng-model遇到的问题
本文给大家分享了一个个人在使用angular过程中遇到的在ng-repeat中使用ng-model的问题,并附上简单的解决办法,希望能对大家学习angular有所帮助2016-01-01
Angular 服务器端渲染错误消息localStorage is not defined解决分析
这篇文章主要为大家介绍了Angular 服务器端渲染错误消息localStorage is not defined解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07
Angular项目里ngsw-config.json文件作用详解
这篇文章主要为大家介绍了Angular项目里ngsw-config.json文件作用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-11-11
Angular使用操作事件指令ng-click传多个参数示例
这篇文章主要介绍了Angular使用操作事件指令ng-click传多个参数,结合实例形式分析了AngularJS事件指令及相关的响应、处理操作技巧,需要的朋友可以参考下2018-03-03
Angular 4依赖注入学习教程之Injectable装饰器(六)
这篇文章主要给大家介绍了关于Angular 4依赖注入之Injectable装饰器的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来跟着小编一起学习学习吧。2017-06-06


最新评论