4

I know we can use fromState and fromParams in the $stateChangeSuccess to get all information about previous location. Like said here:

Angular - ui-router get previous state

$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {
   
});

But is there nice way to parse it into the url / location though? For example:

State: /page/:id/paragraph/:line

Params: id and line, etc..

What I want:

/page/3/paragraph/5 ...

I mean this could get messy with bunch of params. Plus Angularjs ui-router could also have {{ id }} as params too instead of :id. Isn't there a function to do this already or I have to use RegEx? And if so, what is the RegEx string to use?

Thanks.

Community
  • 1
  • 1
HP.
  • 19,226
  • 53
  • 154
  • 253

3 Answers3

9

Use $state.href to get the url:

$rootScope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) {

   // $state.href(from, fromParams)

});

You can also pass options as a third paramerer.
Read the Docs for more information.

Ilan Frumer
  • 32,059
  • 8
  • 70
  • 84
3

Not much differently, but you could do:

$rootScope.$on('$routeChangeStart', function (event, next, current) {
});

And just store the current. The function there is a lot less to deal with than $routeChangesuccess.

gonzofish
  • 1,417
  • 12
  • 19
-2

using window.history.back() will return you to the previous url.

Michael Tot Korsgaard
  • 3,892
  • 11
  • 53
  • 89