How to change $routeParams parameters?

2019-04-28 06:48发布

问题:

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?

回答1:

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



回答2:

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


回答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);