我面临着在两个状态之间传递数据而不暴露网址中的数据的问题,就像用户无法真正直接登陆这个状态一样。
例如。 我有两个状态“A”和“B”。 我正在状态“A”进行一些服务器调用并传递调用的响应 陈述“B”。服务器调用的响应是一条字符串消息,它很长,所以我无法在 url 中公开它。
那么 Angular ui 路由器中有没有什么方法可以在不使用 url 参数的情况下在状态之间传递数据?
最佳答案
我们可以使用 UI-Router 的params
、新功能:
API Reference / ui.router.state / $stateProvider
params
A map which optionally configures parameters declared in the url, or defines additional non-url parameters. For each parameter being configured, add a configuration object keyed to the name of the parameter.
请参阅以下部分:“...或定义其他非 URL 参数...”
所以状态定义是:
$stateProvider
.state('home', {
url: "/home",
templateUrl: 'tpl.html',
params: { hiddenOne: null, }
})
doc mentioned above 的几个例子:
// define a parameter's default value
params: {
param1: { value: "defaultValue" }
}
// shorthand default values
params: {
param1: "defaultValue",
param2: "param2Default"
}
// param will be array []
params: {
param1: { array: true }
}
// handling the default value in url:
params: {
param1: {
value: "defaultId",
squash: true
} }
// squash "defaultValue" to "~"
params: {
param1: {
value: "defaultValue",
squash: "~"
} }
扩展 - 工作示例:http://plnkr.co/edit/inFhDmP42AQyeUBmyIVl?p=info
以下是状态定义的示例:
$stateProvider
.state('home', {
url: "/home",
params : { veryLongParamHome: null, },
...
})
.state('parent', {
url: "/parent",
params : { veryLongParamParent: null, },
...
})
.state('parent.child', {
url: "/child",
params : { veryLongParamChild: null, },
...
})
这可能是使用 ui-sref 的调用:
<a ui-sref="home({veryLongParamHome:'Home--f8d218ae-d998-4aa4-94ee-f27144a21238'
})">home</a>
<a ui-sref="parent({
veryLongParamParent:'Parent--2852f22c-dc85-41af-9064-d365bc4fc822'
})">parent</a>
<a ui-sref="parent.child({
veryLongParamParent:'Parent--0b2a585f-fcef-4462-b656-544e4575fca5',
veryLongParamChild:'Child--f8d218ae-d998-4aa4-94ee-f27144a61238'
})">parent.child</a>
检查示例 here
关于AngularJS ui 路由器在没有 URL 的状态之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248236/