[英]handle browser refresh event with angular 1.5 component routing
我已經實現了Angular 1.5 component routing
。 當用戶通過單擊或鍵盤F5
刷新瀏覽器時,我正在尋找解決方案。 它應將用戶重定向到首頁,並應重置所有輸入/選擇的數據。
這是PLUNKER 。 這只有兩頁。
就我而言,如果用戶確實刷新。 用戶應被重定向到First
,所有輸入/選擇的數據將被重置為默認值。
注意:我知道Angular 1.5組件路由已棄用。 不幸的是,我只有在我的應用程序投入生產后才知道這一點。 我必須為用戶提供一些解決方案,直到我們遷移到Angular2。
這是處理組件路由的代碼:
var module = angular.module('app', ['ngComponentRouter']);
module.config(function($locationProvider) {
$locationProvider.html5Mode(true);
});
module.value('$routerRootComponent', 'myFirstApp');
module.component('myFirstApp', {
templateUrl: "mainview.html",
$routeConfig: [{
path: '/',
redirectTo: ['/First']
}, {
path: '/first',
name: 'First',
component: 'firstComponent'
}, {
path: '/second',
name: 'Second',
component: 'secondComponent'
}]
})
我能夠做到,唯一需要做的就是以編程方式處理路由。 這行是神奇的window.onbeforeunload = $rootRouter.navigate(['First']);
確保注入$rootRouter & $window
服務。
module.component('firstComponent', {
templateUrl: "1.html",
controllerAs: "vm",
controller: function($rootScope, sharedService, $rootRouter, $window) {
window.onbeforeunload = $rootRouter.navigate(['First']);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.