我正在根据当前用户 Angular 色动态加载状态。但是当页面刷新时,它会转到 404 页面。同样在 $stateChangeStart 事件中 fromState.name 是空白的。
有没有办法在点击刷新按钮之前进入状态?我应该在按下刷新之前存储状态然后使用它吗?
.state('404', {
url: '/404',
templateUrl: '404.tmpl.html',
controller: function ($scope, $state, APP) {
$scope.app = APP;
$scope.goHome = function () {
$state.go('default.page');
};
}
})
$urlRouterProvider.otherwise('/404');
....
$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
//fromState.name = '' on refresh
});
提前致谢!
最佳答案
以下似乎有效,不确定这是否是最佳方法。在卸载事件之前保存状态,然后在 $stateChangeStart 中使用它。
.run(function ($rootScope, $window, $state, authService, $http, $timeout, localStorageService) {
$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
if (authService.isLoggedIn()) {
if (toState.name === "404" && fromState.name === '' && localStorageService.get("LAST_STATE") !== "404") {
authService.loadStates($stateProviderRef).then(function () {
$state.go(localStorageService.get("LAST_STATE"), localStorageService.get("LAST_STATE_PARAMS"));
});
}
}
});
window.onbeforeunload = function () {
localStorageService.set("LAST_STATE", $state.current.name);
localStorageService.set("LAST_STATE_PARAMS", $state.params);
return null;
}
})
关于angularjs - 刷新时的 Angular UI 路由器动态状态变为 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34262131/