You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Now I can complete the wizard and save on the WizardSavePage. But what I want is when I'm on WizardPage2 and I navigate to home or contact, I want to show a confirmation "You have unsaved changes. Do you want to continue" or something.
The easiest code would be:
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (!wizardService.saved && current.wizard && !next.wizard) {
if (!confirm("You have unsaved changes. Do you want to continue?")) {
event.preventDefault();
}
}
});
But this does not work, obviously. I could do this with locationChangeStart but then I lose all the route data.
What would be the best way to make something like this?
This is not ideal, but you could just listen on $routeChangeStart and keep references to current and next route. Then when the $locationChangeStart event is broadcasted you can use the kept references to determine if you want to abort or not.
$routeChangeStart always fires before $locationChangeStart.
var currentRoute, nextRoute;
$rootScope.$on("$routeChangeStart", function (event, next, current) {
currentRoute = current.$$route;
nextRoute = next.$$route;
});
$rootScope.$on("$locationChangeStart", function (event, next, current) {
if (!wizardService.saved && currentRoute.wizard && !nextRoute.wizard) {
if (!confirm("You have unsaved changes. Do you want to continue?")) {
event.preventDefault();
}
}
});
I thought there may be some functionality on the routeProvider to get the route data based on a url. Something like:
var routeData = $routeProvider.getRoute('/someUrl/5');
I have just given this a try... It did not work. $locationChangeStart fires before $routeChangeStart.
I should add that I don't use $location.path to navigate but I always use hyperlinks.
@IgorMinar, @yoerlemans so... any other alternative for this? How can I stop the change after the next route has been parsed? As the parseRoute API is not exposed, is my only alternative to copy that code into my app and parse the paths in $locationChangeStart by myself? I need the route definition for checking things like permissions.
Activity
IgorMinar commentedon Jan 3, 2014
This is not ideal, but you could just listen on
$routeChangeStartand keep references to current and next route. Then when the$locationChangeStartevent is broadcasted you can use the kept references to determine if you want to abort or not.$routeChangeStartalways fires before$locationChangeStart.yentheo commentedon Jan 3, 2014
You suggest something like this:
I thought there may be some functionality on the routeProvider to get the route data based on a url. Something like:
Thanks for your answer, btw.
yentheo commentedon Jan 3, 2014
I have just given this a try... It did not work. $locationChangeStart fires before $routeChangeStart.
I should add that I don't use $location.path to navigate but I always use hyperlinks.
Maybe that changes something in the order?
khellang commentedon Jan 7, 2014
👍 For the proposed fix. Too bad I can't use it right away 😄
feat(ngRoute): cancel $routeChangeStart via $event.preventDefault()
caitp commentedon Jan 9, 2014
I was bored and decided to push a clean patch for this @yoerlemans --- but if you reopen a PR I'll take a look at it and close mine.
feat(ngRoute): cancel $routeChangeStart via $event.preventDefault()
feat(ngRoute): cancel $routeChangeStart via $event.preventDefault()
feat(ngRoute): cancel $routeChangeStart via $event.preventDefault()
feat(ngRoute): cancel $routeChangeStart via $event.preventDefault()
jakubsikora commentedon Jan 21, 2014
@IgorMinar, @yoerlemans so... any other alternative for this? How can I stop the change after the next route has been parsed? As the parseRoute API is not exposed, is my only alternative to copy that code into my app and parse the paths in
$locationChangeStartby myself? I need the route definition for checking things like permissions.sinelaw commentedon Jan 27, 2014
+1
khellang commentedon Jan 27, 2014
For anyone who wants to reject a route change (e.g. for authentication/authorization purposes), as a workaround for this issue, check out this plnkr.
mikehayesuk commentedon Apr 5, 2014
+1 👍
romul commentedon Apr 11, 2014
+1 for the proposed fix.
etorres commentedon Apr 19, 2014
+1 clean and nice solution
jamesmorgan commentedon May 8, 2014
+1 👍
geddski commentedon May 22, 2014
#4192 will solve this.
guptapriyank commentedon Jun 10, 2014
+1 helped me too
swlasse commentedon Jun 10, 2014
Thanks for the workaround. Are there any plans for when the fix for this issue will be available in one of the stable builds?
pencilcheck commentedon Aug 10, 2014
@khellang's solution works for me. Thanks.
OakBehringer commentedon Aug 15, 2014
Building on the example provided by @khellang , here is an demo of how you can pass rights/roles to the route provider.
http://embed.plnkr.co/gfF8c0ggklNh9F1c71sF/preview
feat(ngRoute): allow cancelling $route updates via $routeChangeEvent
feat(ngRoute): allow cancelling $route updates via $routeChangeEvent
feat($route): ability to cancel $routeChangeStart event
feat($route): ability to cancel $routeChangeStart event