I know I can get the current URL using $location.search()
and $location.path()
but I need a way to get previous one. Can I use a global variable or something like that?
Asked
Active
Viewed 1.5k times
2

Gianluca Ghettini
- 11,129
- 19
- 93
- 159
-
Would `document.referrer` work for you? In that case this question might be a dupe http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript – Patrick Apr 29 '16 at 08:57
3 Answers
7
Angular $rootScope
will have the all information across your all app components.
Here below $location
service injected on $rootscope
will give you the route information.
$rootScope.$on('$locationChangeStart', function (event, current, previous) {
console.log("Previous URL" +previous);
});

Techie
- 1,491
- 3
- 18
- 24

Sathiya Vasagam S
- 114
- 10
3
You can use for example ui-router to control the flow of your application and with this library it's easy to implement previousState by saving it in $stateChangeSuccess event.

Griffi
- 243
- 2
- 9
0
You can do this when listening on the $routeChangeSuccess event. Parameters are currentRoute and previousRoute. You can also use $locationChangeStart and save the current route yourself.
See here:

Dimi
- 1
- 3