Angularjs gets previous route route

<h1>{{header}}</h1> <!-- This Back button has multiple option --> <!-- In home page it will show menu --> <!-- In other views it will show back link --> <a ng-href="{{back.url}}">{{back.text}}</a> <div ng-view></div> 

In my module configuration

  $routeProvider. when('/', { controller:HomeCtrl, templateUrl:'home.html' }). when('/menu', { controller:MenuCtrl, templateUrl:'menu.html' }). when('/items', { controller:ItemsCtrl, templateUrl:'items.html' }). otherwise({ redirectto:'/' }); 

Controllers

 function HomeCtrl($scope, $rootScope){ $rootScope.header = "Home"; $rootScope.back = {url:'#/menu', text:'Menu'}; } function MenuCtrl($scope, $rootScope){ $rootScope.header = "Menu"; $rootScope.back = {url:'#/', text:'Back'}; } function ItemsCtrl($scope, $rootScope){ $rootScope.header = "Items"; $rootScope.back = {url:'#/', text:'Back'}; } 

As you can see in my controllers, I hardcoded the return URL and text (in fact, I don't need text, like an image). Thus, I found that in some cases the button does not move correctly. I cannot use the history.back() button coz my back to go to the menu link in the home view.

So my question is how to get the previous route route in controllers or the best way to achieve this?

I created a Plunker demo of my problem. Please check it.

+44
angularjs angularjs-routing
Mar 02 '13 at 14:09
source share
8 answers

This alternative also provides inverse function.

Template:

 <a ng-click='back()'>Back</a> 

Module:

 myModule.run(function ($rootScope, $location) { var history = []; $rootScope.$on('$routeChangeSuccess', function() { history.push($location.$$path); }); $rootScope.back = function () { var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/"; $location.path(prevUrl); }; }); 
+66
Sep 19 '13 at 12:41 on
source share
โ€” -

Use the $ locationChangeStart or $ locationChangeSuccess events , the third parameter:

 $scope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) { console.log('start', evt, absNewUrl, absOldUrl); }); $scope.$on('$locationChangeSuccess',function(evt, absNewUrl, absOldUrl) { console.log('success', evt, absNewUrl, absOldUrl); }); 
+21
Jul 23 '13 at 23:44
source share

In your html:

 <a href="javascript:void(0);" ng-click="go_back()">Go Back</a> 

On the main controller:

 $scope.go_back = function() { $window.history.back(); }; 

When the user clicks the "Back" link, the controller function is called and it will return to the previous route.

+13
Apr 21 '16 at 7:09
source share

@andresh For me, locationChangeSuccess worked instead of routeChangeSuccess.

 //Go back to the previous stage with this back() call var history = []; $rootScope.$on('$locationChangeSuccess', function() { history.push($location.$$path); }); $rootScope.back = function () { var prevUrl = history.length > 1 ? history.splice(-2)[0] : "/"; $location.path(prevUrl); history = []; //Delete history array after going back }; 
+4
Jul 11 '15 at 13:36
source share

This is how I now store the link to the previous path in $rootScope :

 run(['$rootScope', function($rootScope) { $rootScope.$on('$locationChangeStart', function() { $rootScope.previousPage = location.pathname; }); }]); 
+3
Feb 23 '15 at 9:15
source share

You will need to associate an event listener with $ rootScope in Angular 1.x, but you will probably need to prove your code a bit in the future without storing the value of the previous location on $ rootScope. The best place to store the value would be the service:

 var app = angular.module('myApp', []) .service('locationHistoryService', function(){ return { previousLocation: null, store: function(location){ this.previousLocation = location; }, get: function(){ return this.previousLocation; } }) .run(['$rootScope', 'locationHistoryService', function($location, locationHistoryService){ $rootScope.$on('$locationChangeSuccess', function(e, newLocation, oldLocation){ locationHistoryService.store(oldLocation); }); }]); 
+3
Nov 03 '15 at 16:59
source share

Just for the document:

The previousRoute callback argument has a property called $route , which is very similar to the $route service. Unfortunately, the currentRoute argument does not contain much information about the current route.

To overcome this, I tried something like this.

 $routeProvider. when('/', { controller:..., templateUrl:'...', routeName:"Home" }). when('/menu', { controller:..., templateUrl:'...', routeName:"Site Menu" }) 

Note that the custom routeName property is added in the above route configurations.

 app.run(function($rootScope, $route){ //Bind the `$routeChangeSuccess` event on the rootScope, so that we dont need to //bind in induvidual controllers. $rootScope.$on('$routeChangeSuccess', function(currentRoute, previousRoute) { //This will give the custom property that we have defined while configuring the routes. console.log($route.current.routeName) }) }) 
0
Mar 06 '13 at 5:34 on
source share

for the code above:

 $scope.$on('$locationChangeStart',function(evt, absNewUrl, absOldUrl) { console.log('prev path: ' + absOldUrl.$$route.originalPath); }); 
0
Feb 23 '14 at 11:10
source share



All Articles