AngularJS:使用UI路由器传递了一个对象进入状态 [英] AngularJS: Pass an object into a state using ui-router

查看:77
本文介绍了AngularJS:使用UI路由器传递了一个对象进入状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够过渡到一个国家,使用的UI路由器传递一个任意对象。

I'd like to be able to transition to a state and a pass an arbitrary object using ui-router.

我知道,通常使用$ stateParams,但我相信这个值是插入网址,我不希望用户能够书签数据。

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',
                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 <一个评论href=\"https://github.com/angular-ui/ui-router/issues/983\">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('富',{URL:/富/?参数1参数2,则params:{参数:
  空} // null是缺省值});

.state('foo', { url: '/foo/:param1?param2', params: { param3: null } // null is the default value });

$ state.go('富',{参数1:'棒',参数2:'巴兹',参数3:{ID:35,
  名称:'什么'}});

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

$ stateParams在'富'是现在{参数1:'棒',参数2:'巴兹',参数3:{
  编号:35,店名:'什么'}}

$stateParams in 'foo' is now { param1: 'bar', param2: 'baz', param3: { id: 35, name: 'what' } }

网址是/ foo /酒吧?参数2 =巴兹。

url is /foo/bar?param2=baz.

这篇关于AngularJS:使用UI路由器传递了一个对象进入状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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