How to change $routeParams parameters?

泄露秘密 提交于 2019-12-12 09:38:37

问题


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


来源:https://stackoverflow.com/questions/14166787/how-to-change-routeparams-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!