angularjs - Angular states - resolve before exiting -
my angular app includes main screen , several tabs , i'm using $stateprovider
switch between states. whenever i'm leaving tab (=state) must perform asynchronous operation. wait operation complete before moving new tab. resolve
done when leaving state rather entering.
how can achieved?
example snippet:
$stateprovider .state('myproject.main', { url: '/main', templateurl: '...', controller: 'mainctrl' }) .state('myproject.tabs.a', { url: '/tab_a', controller: 'tabactrl', templateurl: '...', onexit: dosave }) .state('myproject.tabs.b', { url: '/tab_b', controller: 'tabbctrl', templateurl: '...', onexit: dosave }); function dosave(myservice) { return myservice.save(); //async, promise }
here wanted save (async) whenever switching between tabs. when entering main state tab, want sure save has finished (doing save on myproject.main
's onenter/resolve not me).
problem: when entering myproject.main
(mainctrl), async myservice.save() operation still processing. want completed/resolved.
i tried hack following piece of code, does not work - state change enters loop
.run(function($rootscope, $state, $stateparams, myservice){ $rootscope.$on('$statechangestart', function(ev, tostate, toparams, fromstate, fromparams){ if (tostate.name === 'myproject.main' && //switching main fromstate.name && //for tab fromparams.resume !== true ) { ev.preventdefault(); //stop state change //do save , continue state change myservice.save().then(function(){ $state.go(tostate, {resume: true}) //continue "resume" parameter }); } }); });
thanks!
Comments
Post a Comment