javascript - Handling page refresh in angular ui-router -
when ever reload/refresh page, state routes root page. when checked value of $state.current
after reload 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 value of current state becomes empty on refresh?
Comments
Post a Comment