AngularJS: How to get the previous url and replace its parameters?

1.4k views Asked by At

I am using angular $routeProvider in my application to change the urls. I want to get the previous url and replace some of its route parameters.

$routeProvider.when("/editUser/:userId", {
  templateUrl: "app/user/userEdit.html", 
  controller: "UserCtrl"
});

For example, if my previous url is the above one, I want to change it's parameter userId into something else. (previous url: /editUser/1, I want to change it to: /editUser/2)

Can I do something like that using AngularJS?

1

There are 1 answers

0
Nitheesh On

You can use angular route change events for this purpose, such as

$routeChangeStart, $routeChangeSuccess, $routeChangeError, $routeUpdate

Here is an example

    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        console.log("Next - "  + next.$$route.originalPath);
        console.log("Current - "  + current.$$route.originalPath);
    });

You can store this in any scope values.