arnaud.breton
arnaud.breton

Reputation: 2114

Handling route changes in AngularJS

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.

Upvotes: 2

Views: 4186

Answers (3)

Patrick
Patrick

Reputation: 13974

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

Upvotes: 6

Joseph Oster
Joseph Oster

Reputation: 5545

Keep a model of the dirty state in a parent controller, common 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.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

you could use:

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

See more of it on : $route

Upvotes: 2

Related Questions