codegrid
codegrid

Reputation: 1007

querystring in ionic v1 using angularjs

How do I set a querystring?

sample:

$location.path('/voucher-view?token=' + token);

and how do I retrieve the value?

sample:

var url = $location.search();
        var token = url.token;

sample route:

.state('voucher-view', {
        url: '/voucher-view',
        templateUrl: "templates/voucher-view.html",
        controller: 'VoucherViewController'
    })

Upvotes: 2

Views: 135

Answers (1)

31piy
31piy

Reputation: 23859

I believe you are using Angular UI router. So, to set/get query parameter in the route, you can change your state's configuration like this:

.state('voucher-view', {
  url: '/voucher-view?token',
  templateUrl: "templates/voucher-view.html",
  controller: 'VoucherViewController'
})

Note that I appended the parameter in the URL of the state. Now, when you need to pass the parameter's value, you can simply pass it in the $state.go call:

$state.go('voucher-view', { token: 'your_token_here' });

To retrieve it, you can use $transition$ service to get the value. Inject it in the controller, and use it like this:

$transition$.params().token

If you're using UI Router's 0.x versions, $transition$ will not be available. You need to use $stateParams for it then:

$stateParams.token

Upvotes: 2

Related Questions