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

$routeChangeStart not triggered on load #15698

Closed
@matthieusieben

Description

@matthieusieben

Note: for support questions, please use one of these channels: https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#question. This repository's issues are reserved for feature requests and bug reports.

Do you want to request a feature or report a bug?
Not sure if bug or per-design.

What is the current behavior?
The $locationChangeStart and $locationChangeSuccess are both triggered when the script is bootstrapped. However, if no matching route is found, the $routeChangeStart gets never triggered, not even with null to notify an unmatched initial route.

Since the initial routing is done in an $evalAsync, it is very hacky to be able to detect the case were there is no route matching the initial path:

// $route.current is never set if we do this during bootstrap because it will be executed before the first $locationChangeStart is emitted (i.e. before $route.current is setup)
$scope.$evalAsync(() => {
  if (!$route.current) {
    // redirect to valid route
  }
})
// This will work but will cause flickering if we change stuff ($location.path() or view models)
$timeout(() => {
  if (!$route.current) {
    // redirect to valid route
  }
})
// This can be used to detect invalid initial route:
var unreg = $scope.$on('$locationChangeSuccess', function (event, next) {
  if ($route.current === undefined) {
    $scope.$broadcast('$routeChangeStart', undefined, undefined);
    $scope.$broadcast('$routeChangeError', undefined, undefined, new Error('Unmatched initial route'));
  }
  unreg();
});

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (template: http://plnkr.co/edit/tpl:yBpEi4).

http://plnkr.co/edit/eruQvzIzLWZE1vkdG91s?p=preview

Look at console while loading these urls:

What is the expected behavior?
$routeChangeStart should be triggered with undefined as next route when loading with a non existing path.

What is the motivation / use case for changing the behavior?
Be able to detect incorrect route during bootstrap of the app.

currently I need to have a first listener to $locationChangeSuccess (to be sure that the $route service was initialized, and $route.current is correctly set), to be sure to catch the first routing handling. This listener will do three things:

  • Remove itself
  • Add a listener (routeChangeStartHandler) on $routeChangeStart
  • manually call trigger the routeChange events to act on the current route.

Note that using $routeProvider.otherwise() is not really an option because I wan to be able to restrict some routes depending on user rights, and implement all this logic in a centralized location (i.e. the main controller)

Which versions of AngularJS, and which browser / OS are affected by this issue? Did this work in previous versions of AngularJS? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.

Angular v1.6.2

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions