When the client open the webapp, how to restore his last state with router-ui? -
i use router-ui angular single page
i hear $statechangesuccess event store in localstorage current state , params :
$scope.$on('$statechangesuccess', function(event, tostate) { localstorage.laststate = tostate.name; localstorage.laststateparams = json.stringify($stateparams); });
my question : when client open webapp, want restore personnal last state can put code ?
$state.go(localstorage.laststate, json.parse(localstorage.laststateparams));
// update put code in $statechangesuccess event :
$scope.$on('$statechangesuccess', function(event, tostate) { if (tostate.name !== 'welcome') { localstorage.laststate = tostate.name; localstorage.laststateparams = json.stringify($stateparams); } if (tostate.name !== localstorage.laststate) { $state.go(localstorage.laststate, json.parse(localstorage.laststateparams)); } });
the problem solution process @ opening app :
- localhost (user open app)
- localhost/#/welcome ( because $urlrouterprovider.otherwise('/welcome') proc before $statechangesuccess event)
- and : localhost/#/articles/423 (on $statechangesuccess event)
so i'm sure bad, know method ?
thanks much,
as solution easier think !
in main controller :
$scope.$on('$statechangesuccess', function(event, tostate) { // timeout required because document.location.hash not upadated immediatly $timeout(function() { localstorage.hash = document.location.hash.substr(1); }, 100); });
at end of routes :
// default $urlrouterprovider.otherwise(localstorage.hash || '/welcome');
Comments
Post a Comment