21451

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

  • How to handle browser back button event in a particular controller?
  • RangeError: Maximum call stack size exceeded (AngularJS)
  • how to pass the value from one page to another page in angular?
  • How to inject ngRoute into Jasmine / Karma AngularJS unit test?
  • How to show star-rating dynamically based on response?
  • Do I need to clean user input for DB::query calls in laravel?
  • Can't remove inline event handler in chrome
  • How to test method of JavaFX controller?
  • $stateChangeStart don't work when user refresh browser
  • How to “remove”/“change” some require(…) calls when using browserify?
  • Spring integration inbound-gateway Fire an event when queue is empty
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Salesforce Different WSDL files and when to use
  • TFS 2015 - Waiting for an agent to be requested
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • How do I include a SWC in an AS2 Flash project?
  • Simulate click Geckofx vb,net
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Specifying Castle WCF Integration Facility Endpoint Behavior per Endpoint
  • Zurb Foundation _global.scss meta styles for js?
  • AJAX Html Editor Extender upload image appearing blank
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Change JButton Shape while respecting Look And Feel
  • Is calc() supported in html email?
  • req.body is undefined - nodejs
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Is there a amazon webstore API for customers?
  • Updating server-side rendering client-side
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Circular dependency while pushing http interceptor
  • Is there a mandatory requirement to switch app.yaml?
  • Angular 2 constructor injection vs direct access
  • Linking SubReports Without LinkChild/LinkMaster
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?