2

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

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.