【问题标题】:querystring in ionic v1 using angularjsionic v1中使用angularjs的查询字符串
【发布时间】:2018-05-28 08:02:12
【问题描述】:

如何设置查询字符串?

样本:

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

以及如何检索该值?

样本:

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

示例路线:

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

【问题讨论】:

    标签: angularjs ionic-framework angular-ui-router


    【解决方案1】:

    我相信您正在使用 Angular UI 路由器。因此,要在路由中设置/获取查询参数,您可以像这样更改状态的配置:

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

    请注意,我在状态的 URL 中附加了参数。现在,当您需要传递参数的值时,只需在$state.go 调用中传递即可:

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

    要检索它,您可以使用$transition$ 服务来获取值。将其注入控制器中,并像这样使用它:

    $transition$.params().token
    

    如果您使用的是 UI Router 的 0.x 版本,$transition$ 将不可用。然后你需要使用$stateParams

    $stateParams.token
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 2013-06-02
      • 2020-01-11
      相关资源
      最近更新 更多