How to change $routeParams parameters?

All we need is an easy explanation of the problem, so here it is.

Im accessing this url: http://bazarak.af/#!/annonser/page=1&

.when("/annonser/:query",{
    templateUrl: '/views/search.php',
    controller: "Search"
})

I would like to get the value of page and change it both in the url and in the routeParams.

To get the page value I have tried

var pageNumber = $routeParams.page;  

and to set it I have tried:

$routeParams ==> {page:5}

None of this works. And the documentation is not really helping me.
How can I do this?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Using $location.path() requires that you can give it the hole path including search params and hash so you would need to parse the url in order to change a single parameter in the middle of the url such as this one /user/:id/inbox/:read then its easier to use:

// given the current url is http://example.com/user/3/inbox/unread?sort=desc
$route.updateParams({id:"2"});
// will become http://example.com/user/2/inbox/unread?sort=desc

Method 2

Use $location.path() to set new URL path. It will be reflected in routes as well.
You can also just change $location.search to change your query
Read this documentation
http://docs.angularjs.org/guide/dev_guide.services.$location

Method 3

You can also use the following to clear all the route parameters from the url:

var myParams = new Object();
for(var param in $routeParams){
 myParams[param] = null;
}
$route.updateParams(myParams);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply