Angularjs實(shí)現(xiàn)頁(yè)面模板清除的方法
前幾天項(xiàng)目在上線過(guò)程中,出現(xiàn)了一些新問(wèn)題。頁(yè)面在切換時(shí)由于前一個(gè)頁(yè)面的模板清理不及時(shí),會(huì)造成頁(yè)面的重疊。導(dǎo)致這個(gè)問(wèn)題的原因是:頁(yè)面模板緩存,即上一個(gè)頁(yè)面退出時(shí),瀏覽器沒(méi)有及時(shí)清空上一個(gè)頁(yè)面的模板,導(dǎo)致新頁(yè)面加載時(shí),舊頁(yè)面模板依然存在,從而頁(yè)面出現(xiàn)重疊。
模板緩存清除:
模板緩存的清除包括傳統(tǒng)的 HTML標(biāo)簽設(shè)置清除緩存,以及angularJs的一些配置清除,和angularJs的路由切換清除
1、以下是傳統(tǒng)的清除瀏覽器的方法
HTMLmeta標(biāo)簽設(shè)置清除緩存
<!-- 清除緩存 --> <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表單臨時(shí)緩存
<body onLoad="javascript:document.formName.reset()">
2、angularJs配置清除緩存
1、清除路由緩存,在route路由配置中,注入$httpProvider服務(wù),通過(guò)$httpProvider服務(wù)配置,清除路由緩存。
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、用隨機(jī)數(shù),隨機(jī)數(shù)也是一種很不錯(cuò)避免緩存的的方法,即在鏈接 URL 參數(shù)后加上隨機(jī)數(shù)(一般加時(shí)間戳) 。用隨機(jī)時(shí)間,和隨機(jī)數(shù)一樣。
3、在狀態(tài)路由配置中,將cache配置項(xiàng),配置為false。
.state("discountCoupon", { url: "/discountCoupon", templateUrl: "discountCoupon.html?" + new Date().getTime(), //隨機(jī)數(shù) controller: 'discountCoupon', cache: false, //cache配置 }) .state("customerPhone", { url: "/customerPhone", templateUrl: "customerPhone.html?" + new Date().getTime(), //隨機(jī)數(shù) controller: 'customerPhone', cache: false, //cache配置 })
3、angularJs的路由切換清除緩存
angularJs默認(rèn) 模板加載都會(huì)被緩存起來(lái),使用的緩存服務(wù)是 $tempalteCache, 發(fā)送模板請(qǐng)求的服務(wù)是$templateRequest,所以可以在路由切換時(shí)將上一個(gè)頁(yè)面的模板清除:
1.每次發(fā)送 $http 請(qǐng)求模板完成后,可以調(diào)用 $tempalteCache.remove(url) 或 $tempalteCache. removeAll 清除所有模板緩存。
$rootScope.$on('$stateChangeStart', //路由開(kāi)始切換 function (event, toState, toParams, fromState, fromParams) { //路由開(kāi)始切換,清除以前所有模板緩存 if (fromState.templateUrl !== undefined) { $templateCache.remove(fromState.templateUrl); // $templateCache.removeAll(); } }); $rootScope.$on('$stateChangeSuccess', //路由切換完成 function (event, toState, toParams, fromState, fromParams) { //路由切換成功,清除上一個(gè)頁(yè)面模板緩存 if (fromState.templateUrl !== undefined) { $templateCache.remove(fromState.templateUrl); // $templateCache.removeAll(); } });
2.使用 $provide.decorator
改寫原生的 $templateRequest
(angularJs 自帶 $provide服務(wù)里 $templateRequest: $TemplateRequestProvider)服務(wù)。在 $TemplateRequestProvider 服務(wù)里面我們可以看到默認(rèn)使用了 $tempalteCache (本質(zhì)還是 angularJs 的 $cacheFactory 服務(wù)) 服務(wù),
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) { function handleRequestFn(tpl, ignoreRequestError) { handleRequestFn.totalPendingRequests++;
并在獲取模板時(shí),默認(rèn)以 $templateCache 作為 cache使用,將獲取到的模板數(shù)據(jù),添加到 $templateCache內(nèi)保存。
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);
所以可以通過(guò)禁掉緩存,在 $templateRequest 的源碼中將 $tempalteCache去掉,達(dá)到清除模板緩存的目的,不過(guò)這個(gè)一般不建議直接修改框架源代碼!
總結(jié)
以上所述是小編給大家介紹的Angularjs實(shí)現(xiàn)頁(yè)面模板清除的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
MODULE_INITIALIZER初始化Angular?懶加載模塊高級(jí)技巧
這篇文章主要為大家介紹了MODULE_INITIALIZER初始化Angular懶加載模塊高級(jí)技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Angular Universal服務(wù)器端渲染避免 window is not&
這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯(cuò)誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07AngularJS實(shí)現(xiàn)表單驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Angular中$broadcast和$emit的使用方法詳解
本篇文章主要介紹了Angular中$broadcast和$emit的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Angular 4依賴注入學(xué)習(xí)教程之ClassProvider的使用(三)
這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之ClassProvider使用的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Angular 4具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06Angular企業(yè)級(jí)開(kāi)發(fā)——MVC之控制器詳解
本篇文章主要介紹了Angular企業(yè)級(jí)開(kāi)發(fā)——MVC之控制器詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02