How can I retrieve the previous URL in AngularJS

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?

Answer1:

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); });

Answer2:

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.

Answer3:

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

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:How can I retrieve the previous URL in AngularJS