I'm a newbie to Angular. Currently I've a challenge I'm been working for hours. I thought of posting here. The problem is how can I preserve the query string value when the route changes in Angular. I'm using the ui router. The querystring has an uid that will be send in each request which I could able achieve through httpinterceptor. But I really got struck up in preserving the uid in querystring whenever the route changes. Can anyone give some insights on this please?
1 Answer
When using ui router you (usually) specify the url, template and controller for that state:
.state('mystate', {
url: "/mystate",
template: "<p>Some template content using scope: {{title}}</p>",
controller: function($scope) {
$scope.title = "State 1";
}
});
To preserve the query string between states you can add a state param to your states:
.state('mystate', {
url: "/mystate?myParam",
...
...
},
You can then access the parameter in the state controller with $stateParams.myParam.
Note: You have to pass myParam when changing state:
$state.go("mystate", {myParam: "yourValueGoesHere"});
9 Comments
Nikolai
I believe it has to be
url: "/mystate/:myParam", rather than url: "/mystate?myParam",amu
You can actually use either one:
url: "/mystate/:myParam" for URL parameters and url: "/mystate?myParam" for query parameters.Nikolai
Yes but using ? is not conventional as I understand...maybe I'm wrong though
amu
I agree that using
/user/:id is preferable to /user?id. However, sometimes you might want to pass several / other params that are not so obvious or compliant with the RESTful architecture. Eg. /cars/toyota?limit=25Nikolai
you could do it this way:
url: "/carState/:carMake/:limit" |