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

event.preventDefault() does not work in $routeChangeStart after $route.reload() #9824

Closed
ldvcgllm opened this issue Oct 29, 2014 · 3 comments

Comments

@ldvcgllm
Copy link

Angular version 1.3.0

Failing code

    $rootScope.$on('$routeChangeStart', function(event, curr, prev) {
        if (...condition1...) {
            event.preventDefault();
            // do something to get condition1 == false
            $route.reload();
        }

        if (...condition2...) {
            event.preventDefault(); // <-- this is not preventing
            // do something to get condition2 == false
            $route.reload();
        }
    });

Issue in source

      reload: function() {
        forceReload = true;
        $rootScope.$evalAsync(function() {
          // Don't support cancellation of a reload for now...
          prepareRoute(); // <-- param is empty
          commitRoute();
        });
      },

...

function prepareRoute($locationEvent) {
  var lastRoute = $route.current;

  preparedRoute = parseRoute();
  preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute.$$route === lastRoute.$$route
      && angular.equals(preparedRoute.pathParams, lastRoute.pathParams)
      && !preparedRoute.reloadOnSearch && !forceReload;

  if (!preparedRouteIsUpdateOnly && (lastRoute || preparedRoute)) {
    if ($rootScope.$broadcast('$routeChangeStart', preparedRoute, lastRoute).defaultPrevented) {
      if ($locationEvent) { <-- is undefined
        $locationEvent.preventDefault();
      }
    }
  }
}
@caitp
Copy link
Contributor

caitp commented Oct 29, 2014

@tbosch we should probably check if we need to reload the route after preventDefault, in case it was called during the handler

@naomiblack
Copy link
Contributor

adding a tag because this is an issue for one of our larger internal teams...

@petebacondarwin petebacondarwin modified the milestones: 1.5.1, Backlog Jan 29, 2016
@petebacondarwin
Copy link
Member

Is it possible to provide a running example of this issue?
Or even better a failing unit test?

@petebacondarwin petebacondarwin self-assigned this Jan 29, 2016
gkalpak added a commit to gkalpak/angular.js that referenced this issue Jan 30, 2016
gkalpak added a commit to gkalpak/angular.js that referenced this issue Jan 30, 2016
@gkalpak gkalpak closed this as completed in 2f0a50b Feb 2, 2016
gkalpak added a commit that referenced this issue Feb 2, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants