尝试将参数传递给我的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?
答案 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
答案 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并且不安全,内部状态必须从用户隐藏