gpt4 book ai didi

javascript - 如何使用 UI-router 将对象传递到状态?

转载 作者:IT王子 更新时间:2023-10-29 02:39:11 25 4
gpt4 key购买 nike

我希望能够使用 ui-router 转换到状态并传递任意对象。

我知道通常使用 $stateParams,但我相信这个值已插入到 URL 中,我不希望用户能够将此数据添加为书签。

我想做这样的事情。

$state.transitionTo('newState', {myObj: {foo: 'bar'}});

function myCtrl($stateParams) {
console.log($stateParams.myObj); // -> {foo: 'bar'}
};

有没有办法在不将值编码到 URL 的情况下执行此操作?

最佳答案

在 0.2.13 版本中,您应该能够将对象传递到 $state.go,

$state.go('myState', {myParam: {some: 'thing'}})

$stateProvider.state('myState', {
url: '/myState/{myParam:json}',
params: {myParam: null}, ...

然后访问 Controller 中的参数。

$stateParams.myParam //should be {some: 'thing'}

myParam 不会出现在 URL 中。

来源:

查看 christopherthielen 的评论 https://github.com/angular-ui/ui-router/issues/983 ,为方便起见转载于此:

christopherthielen: Yes, this should be working now in 0.2.13.

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

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

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

url is /foo/bar?param2=baz.

关于javascript - 如何使用 UI-router 将对象传递到状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632255/

25 4 0
文章推荐: xml - XSL 1.0 使用当前值检查变量字符串
文章推荐: javascript - 如何将 Typescript 与原生 ES6 Promises 结合使用
文章推荐: xml - 如何在 Sublime Text 3 的 XML 引号内突出显示 LUA 语法?
文章推荐: javascript - 动态创建