如何使用 UI-router 将对象传递到状态? [英] How to pass an object into a state using UI-router?

查看:26
本文介绍了如何使用 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,名称:'什么' } });

$state.go('foo', { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } });

'foo' 中的 $stateParams 现在是 { param1: 'bar', param2: 'baz', param3: {id: 35, name: '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-router 将对象传递到状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆