如何使用UI路由器将对象传递到状态? [英] How to pass an object into a state using UI-router?
问题描述
我希望能够使用ui-router转换为状态并传递任意对象.
I'd like to be able to transition to a state and a pass an arbitrary object using ui-router.
我知道通常会使用$stateParams
,但是我相信此值会插入URL中,并且我不希望用户能够对此数据添加书签.
I'm aware that usually $stateParams
is used, but I believe this value is inserted into the URL, and I don't want users to be able to bookmark this data.
我想做这样的事情.
$state.transitionTo('newState', {myObj: {foo: 'bar'}});
function myCtrl($stateParams) {
console.log($stateParams.myObj); // -> {foo: 'bar'}
};
是否可以在不将值编码到URL的情况下做到这一点?
Is there a way to do this without encoding values into the URL?
推荐答案
在0.2.13版中,您应该能够将对象传递到$ state.go,
In version 0.2.13, You should be able to pass objects into $state.go,
$state.go('myState', {myParam: {some: 'thing'}})
$stateProvider.state('myState', {
url: '/myState/{myParam:json}',
params: {myParam: null}, ...
,然后访问控制器中的参数.
and then access the parameter in your controller.
$stateParams.myParam //should be {some: 'thing'}
myParam将不会显示在URL中.
myParam will not show up in the URL.
来源:
请参阅christopherthielen的评论 https://github.com/angular-ui/ui-router/issues/983 ,为方便起见,在此处复制:
See the comment by christopherthielen https://github.com/angular-ui/ui-router/issues/983, reproduced here for convenience:
christopherthielen:是的,它现在应该在0.2.13中起作用.
christopherthielen: Yes, this should be working now in 0.2.13.
.state('foo',{url:'/foo/:param1?param2',params:{param3: null}//null为默认值});
.state('foo', { url: '/foo/:param1?param2', params: { param3: null } // null is the default value });
$ state.go('foo',{param1:'bar',param2:'baz',param3:{id:35, 名称:'what'}});
$state.go('foo', { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } });
'foo'中的$ stateParams现在为{param1:'bar',param2:'baz',param3:{ id:35,名称:"what"}}
$stateParams in 'foo' is now { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } }
URL是/foo/bar?param2 = baz.
url is /foo/bar?param2=baz.
这篇关于如何使用UI路由器将对象传递到状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!