如何将状态参数传递给ui-router?

时间:2015-08-29 07:26:19

标签: angularjs routing angular-ui-router angularjs-routing

尝试将参数传递给我的ui路由器:

$state.go('orderDetail', {myParam: {accountID: $scope.selectedAccount}})

在我的stateprovider orderDetail州看起来像这样:

.state('orderDetail', {
    templateUrl: 'modules/common/orders/partials/detail.html?referrer',
    controller: 'OrderDetailCtrl',
    url: '/detail/:myParam',
    resolve: {

        orderDetails: function (myservice, configService, $stateParams) {
            console.log('resolve');
            var referrer = $stateParams.myParam;
            debugger;
            console.log('stateParm', referrer.accountID);
            console.log('order detail resolving');
            //todo remove hardcoded
            return myservice.getDetail(configService.config('mock_order_detail').url + '?accountId=2233');
        }
    }
});

console.log('stateParm', referrer.accountID);显示为undefined。我如何传递accountID param?

2 个答案:

答案 0 :(得分:0)

要识别myParam是对象的状态,您需要在状态params选项中定义其默认值,方法是为其分配null或任何默认值。< / p>

<强>代码

.state('orderDetail', {
      templateUrl: 'modules/common/orders/partials/detail.html?referrer',
      controller: 'OrderDetailCtrl',
      url: '/detail/:myParam',
      resolve: {
           //resolve code here
      }
      params: {
          'myParam': null
      }
}
  

要使上述更改生效,您应该将ui-router版本更新为 0.2.13

Github Issue Link

答案 1 :(得分:0)

$ stateParams只接受字符串,但你可以将你的对象字符串化为myParam

$state.go('orderDetail', {myParam: angular.fromJson({accountID: $scope.selectedAccount})})

然后

.state('orderDetail', {
    templateUrl: 'modules/common/orders/partials/detail.html?referrer',
    controller: 'OrderDetailCtrl',
    url: '/detail/:myParam',
    resolve: {
        orderDetails: function (myservice, configService, $stateParams) {
            var referrer = angular.toJson($stateParams.myParam);
            return myservice.getDetail(configService.config('mock_order_detail').url + '?accountId=2233');
        }
    }
});

但是我不建议使用字符串化的对象,更好地使用服务作为状态的桥梁,只是因为制作丑陋的URL并且不安全,内部状态必须从用户隐藏