1

I am trying to store the "next route" argument of $routeChangeStart inside a service and call $location.path() with the "next route" from a different place. To clarify here some code:

$scope.$on('$routeChangeStart', function (event, next, current) {
    service.nextRoute = next;
    $rootScope.$broadcast('event:someEvent');
});

...now I catch the event and try to do something like (assume that the services used are all properly injected and available):

$rootScope.$on('event:someEvent', function() {
    //do some stuff
    $location.path(service.nextRoute);
});

what I get is a TypeError: Object #<Object> has no method 'charAt' error from the call to $locaton.path(service.nextRoute);. There is another post here on StackOverflow with a similar problem but the proposed solution doesn't seem to work? So what am I doing wrong?

Thanks

Community
  • 1
  • 1
evermean
  • 1,255
  • 21
  • 49

1 Answers1

1

path() take a string where next is a route {} object. It sounds like you actually want to capture $locationChangeStart instead which will give you access to the path.

scope.$on('$locationChangeStart', function(event, newVal, oldVal) {
});

It is an undocumented feature according to this answer.

Community
  • 1
  • 1
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101