Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngRoute): cancel $routeChangeStart via $event.preventDefault() #5714

Closed
wants to merge 1 commit into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Jan 9, 2014

This change allows ngRoute route changes to be cancelled using
$event.preventDefault.

This enables route changes to be aborted at the discretion of the application.

Route changes may not be cancelled if forceReload is set to true.

Closes #5581

@IgorMinar
Copy link
Contributor

but this doesn't restore the $location.url() value, so now we have this weird state in which the url doesn't match the current route according to the route definition.

I think that if we do allow route changes to be aborted then we should restore the $location state as well.

This issue is one of the reasons why we currently allow $location changes to be aborted but don't offer this functionality at the $route layer.

@IgorMinar
Copy link
Contributor

I think it's fixable though... :)

@caitp
Copy link
Contributor Author

caitp commented Jan 10, 2014

I'm not totally sure how to go about asking the history api to go back/unpush state if it needs to within this framework, which seems like it might be a nice thing.

This change allows ngRoute route changes to be cancelled using $event.preventDefault.

This enables route changes to be aborted at the discretion of the application.

Route changes may not be cancelled if forceReload is set to true.

Closes angular#5581
@caitp caitp mentioned this pull request Jan 17, 2014
@jakubsikora
Copy link

@caitp, @yoerlemans good luck! I'm howling as well for a fix for that situation.

@JobaDiniz
Copy link

why this is not already integrated? It would be nice to have it on 1.2.x releases.

@caitp
Copy link
Contributor Author

caitp commented Jun 16, 2014

oh look at that, it's bitrotten! anyways, I think an alternative solution had been proposed, but I can't recall where (I think Vojta was looking at it).

So this might not be the one that ships, but I'll see if we can ship one of the solutions for the next release

@bsandrow
Copy link

@IgorMinar I am interested in this functionality as a way to prevent reloading of the entire ng-view when a route changes from (e.g.) /product/1 to /product/2. In this case, the only 'route change' is the product id. I want the path to change /product/1 => /product/2 but I would want the ability to only reload certain data (or reload data and swap it out in the $scope). If messing with $routeChangeStart and not reverting $location is considered bad, then what is the preferred way of doing this?

@IgorMinar IgorMinar modified the milestones: 1.3.0-rc.5, 1.3.0 Oct 2, 2014
caitp added a commit to caitp/angular.js that referenced this pull request Oct 3, 2014
Previously, one could perform this feat by listening to both
$locationChangeStart as well as $routeChangeStart, and being careful to update
the route correctly. Now, it's possible to simply preventDefault() on the
$beforeRouteChange event in order to prevent location and route from being
updated.

While this does not cancel $routeChangeStart, it does ensure that the correct
location is restored, and provides the same information as $routeChangeStart.

Closes angular#5855
Closes angular#5714
Closes angular#5581
caitp added a commit to caitp/angular.js that referenced this pull request Oct 3, 2014
Previously, one could perform this feat by listening to both
$locationChangeStart as well as $routeChangeStart, and being careful to update
the route correctly. Now, it's possible to simply preventDefault() on the
$beforeRouteChange event in order to prevent location and route from being
updated.

While this does not cancel $routeChangeStart, it does ensure that the correct
location is restored, and provides the same information as $routeChangeStart.

Closes angular#5855
Closes angular#5714
Closes angular#5581
@jeffbcross jeffbcross force-pushed the master branch 2 times, most recently from abdaab7 to 30996f8 Compare October 8, 2014 19:46
tbosch added a commit to tbosch/angular.js that referenced this pull request Oct 8, 2014
Calling `preventDefault()` on a `$routeChangeStart` event will
prevent the route change and also call `preventDefault` on the `$locationChangeStart` event, which prevents the location change as well.

BREAKING CHANGE:

Order of events has changed.
Previously: `$locationChangeStart` -> `$locationChangeSuccess` 
  -> `$routeChangeStart` -> `$routeChangeSuccess`

Now: `$locationChangeStart` -> `$routeChangeStart` 
  -> `$locationChangeSuccess` ->  -> `$routeChangeSuccess`

Fixes angular#5581
Closes angular#5714
@tbosch tbosch closed this in f4ff11b Oct 8, 2014
bullgare pushed a commit to bullgare/angular.js that referenced this pull request Oct 9, 2014
Calling `preventDefault()` on a `$routeChangeStart` event will
prevent the route change and also call `preventDefault` on the `$locationChangeStart` event, which prevents the location change as well.

BREAKING CHANGE:

Order of events has changed.
Previously: `$locationChangeStart` -> `$locationChangeSuccess`
  -> `$routeChangeStart` -> `$routeChangeSuccess`

Now: `$locationChangeStart` -> `$routeChangeStart`
  -> `$locationChangeSuccess` ->  -> `$routeChangeSuccess`

Fixes angular#5581
Closes angular#5714
Closes angular#9502
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to cancel $routeChangeStart event
5 participants