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 so
$location.search()
$location.path()
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:
AngularJS $route
AngularJS $location