querystring in ionic v1 using angularjs

Issue

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

Solution

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

Answered By – 31piy

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.