Raz Buchnik
Raz Buchnik

Reputation: 8401

How to pass query string parameters through URL

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.

Upvotes: 0

Views: 333

Answers (3)

Raja Ramachandran
Raja Ramachandran

Reputation: 456

Url config use like this

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

you get value in controller use to $stateParams

Upvotes: 1

Raz Buchnik
Raz Buchnik

Reputation: 8401

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

Upvotes: 0

NTP
NTP

Reputation: 4448

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

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

Upvotes: 2

Related Questions