最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handling route changes in AngularJS - Stack Overflow

programmeradmin0浏览0评论

I'm struggling to handle the back button and ask confirmation to the user, if app state is dirty. Closing tab and reload is already handled via the onbeforeunload hook.

I've played with $routeChangeStart, $beforeRouteChange, etc. but can't handle it correctly. With $routeChangeStart, the route is effectively changed before my callback is called.

What is the correct way to handle this situation?

Thanks for your help.

I'm struggling to handle the back button and ask confirmation to the user, if app state is dirty. Closing tab and reload is already handled via the onbeforeunload hook.

I've played with $routeChangeStart, $beforeRouteChange, etc. but can't handle it correctly. With $routeChangeStart, the route is effectively changed before my callback is called.

What is the correct way to handle this situation?

Thanks for your help.

Share Improve this question edited Apr 22, 2013 at 7:05 arnaud.breton asked Apr 22, 2013 at 6:04 arnaud.bretonarnaud.breton 2,1142 gold badges27 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

I believe $locationChangeStart fires when you would like it to.

you could use:

$scope.$on('$routeChangeStart', function(next, current) { 
   //broadcasted before a route change
   // do something here
});

See more of it on : $route

Keep a model of the dirty state in a parent controller, mon to all views? An alert/confirm appears if a dirty state is detected by any view's controller, with a "Cancel" button that resets $location.path(...) to the dirty page.

发布评论

评论列表(0)

  1. 暂无评论