Handling page refresh in angular ui-router

北城余情 提交于 2019-12-13 06:12:06

问题


When ever I reload/refresh the page, the state routes back to root page. When I checked the value of $state.current after reload it is empty.

currentState:

Object {name: "", url: "^", views: null, abstract: true}

States:

angular.module('App', ['ngCookies','ngResource','ui.router'])
.config(function ($stateProvider, $urlRouterProvider) {

$stateProvider
    .state('login', {
        url: '/login',
        templateUrl: "views/login.html",
        data: {
            required: false
        }
    })
    .state('dashboard', {
        url: '/dashboard',
        templateUrl: "views/dashboard.html",
        data: {
            required: true
        }
    });
$urlRouterProvider.otherwise('/login');
})

.run(["$rootScope", "$state", "$location", "authService", "$cookies", "$sessionStorage", function ($rootScope, $state, $location, authService, $cookies, $sessionStorage) {
$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
    console.log("currentState: ", $state.current);
    console.log("toState is:", toState);

    if (toState.data.required && !$sessionStorage.authuserid) {
        alert("state not authenticated");
        e.preventDefault();
        $state.go('login');
    }
});
}]);

why the value of current state becomes empty on refresh?

来源:https://stackoverflow.com/questions/37500569/handling-page-refresh-in-angular-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!