0

I am using this:

$state.go("/path/to");

I want to do this:

$state.go("/path/to?param=myParam");

But this is not working.

I have tried:

$location.path("/path/to?param=myParam");
$location.path("/path/to").search({param: "myParam"});

Both of them actually change the url but not updated the angularjs app - since this is not connected to the actual router.

How can I pass query string parameters using AngularJS ui router? Note I know I can pass a full url path like this:

/path/to/param/myParam 

But I need in this case that it will be query string params.

1
  • you can do this .state('path', { url: '/to/:param', template: '<res-template></res-template>' }) Commented Jul 23, 2019 at 8:23

3 Answers 3

2

According to the documentation $state.go takes another parameter that you can use to pass query string.

$state.go("/path/to",{param:myParam});
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't working. I am naming the name of the state the same as the url of it. Does it matters?
1

Url config use like this

  .state('path/:param', { 
                url: '/path/:param',
                templateUrl: 'views/path.html',
      }) 

you get value in controller use to $stateParams

1 Comment

While your code may provide the answer to the question, please add context around it so others will have some idea what it does and why it is there.
0

I don't know why, but angularjs ui router is very tedious vs the regular ngroute made by Angular team. The reason I am using ui router is just because that it supports for lazyload of the view.

Anyway, this is the solution I was found - using $state.go and $location.path both:

$location.path(`/path/to`).search({param: "myParam}});
$state.go(`/path/to`);

This can be wrapped for easy use:

module.exports = (path, qsObj) => {
    if (qsObj) {
      $location.path(path).search(qsObj);
    }
    $state.go(path);
}

Comments

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.