0

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 1

1

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

Read more in ui router doc

Sign up to request clarification or add additional context in comments.

9 Comments

I believe it has to be url: "/mystate/:myParam", rather than url: "/mystate?myParam",
You can actually use either one: url: "/mystate/:myParam" for URL parameters and url: "/mystate?myParam" for query parameters.
Yes but using ? is not conventional as I understand...maybe I'm wrong though
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=25
you could do it this way: url: "/carState/:carMake/:limit"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.