evermean
evermean

Reputation: 1307

Change AngularJS location with stored route

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

Upvotes: 1

Views: 259

Answers (1)

Mark Coleman
Mark Coleman

Reputation: 40863

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.

Upvotes: 1

Related Questions