访问自定义数据中的参数 [英] Accessing parameters in custom data
问题描述
我正在使用自定义数据为我的状态添加一个人类可读的名称,如下所示:
I'm using custom data to add a human readable name to my states, like this:
.state('data', {
parent: 'week',
url: '/data',
displayName: 'Data Overview',
然后我可以像这样在模板中使用它:
I can then use it in the template like this:
<h1>Showing {{$state.current.displayName}}</h1>
这很好.但是现在我想创建一个带有参数的路由,并在 displayName 中使用该参数:
Which is nice. But now I want to create a route with a parameter, and use that parameter in the displayName:
.state('upload', {
parent: 'week',
url: '/upload/:level',
displayName: 'Data Upload for ' + level,
...
但我无法找出使用访问 displayName
中的 level
值的正确语法.
But I can't figure out the correct syntax to use access the value of level
inside the displayName
.
推荐答案
static
数据 (建议放在 data 内:{}
- 不直接在根状态设置上) - 是静态数据.所以:
The static
data (suggested to be placed inside of data : {}
- not to be directly on the root state settings) - are static data. So:
没有NO方法如何动态设置这些数据.例如.基于 $stateParams
.这些设置应该在 .config()
阶段定义,而不是在 .run()
中进行评估(与 resolve、templateUrl...)
there is NO way how to set these data dynamically. E.g. based on the
$stateParams
. These settings are expected to be defined in the.config()
phase, not evaluated in.run()
(in compariosn with others like resolve, templateUrl...)
查看文档:
您可以将自定义数据附加到状态对象 (我们建议使用数据属性以避免冲突).
You can attach custom data to the state object (we recommend using a data property to avoid conflicts).
// Example shows an object-based state and a string-based state
var contacts = {
name: 'contacts',
templateUrl: 'contacts.html',
data: {
customData1: 5,
customData2: "blue"
}
}
$stateProvider
.state(contacts)
.state('contacts.list', {
templateUrl: 'contacts.list.html',
data: {
customData1: 44,
customData2: "red"
}
})
使用上面的示例状态,您可以像这样访问数据:
With the above example states you could access the data like this:
function Ctrl($state){
console.log($state.current.data.customData1) // outputs 5;
console.log($state.current.data.customData2) // outputs "blue";
}
这篇关于访问自定义数据中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!