在自定义数据访问参数 [英] Accessing parameters in custom data

查看:115
本文介绍了在自定义数据访问参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义数据向人类可读的名字添加到我的国家,像这样的:

  .STATE('数据',{
  家长:'周',
  网址:'/数据',
  显示名:数据概览,

然后我可以使用它在这样的模板:

 < H1>显示{{$ state.current.displayName}}< / H1>

这是很好的。但现在我想创建一个带参数的路线,并使用该参数的显示名:

  .STATE('上传',{
  家长:'周',
  网址:'/上传/:级别,
  显示名:数据上传+级,
  ...

但我不能找出正确的语法使用访问值级别中的显示名


解决方案

静态数据的(建议放在的内部数据{} - 不要直接放在根状态设置)的 - 是的静态数据。所以:


  

办法如何动态地设置这些数据。例如。根据 $ stateParams 。预计这些设置在的.config()阶段被定义,在 .RUN不计算() (与其他人一样的决心compariosn,templateUrl ...)


查看DOC:

安装自定义数据到状态对象


  

您可以将自定义数据的状态对象的(我们建议您使用
  数据属性来避免冲突)。

  //示例显示了基于对象的状态和基于字符串的状态
VAR接触= {
    名称:'联系人',
    templateUrl:contacts.html',
    数据:{
        customData1:5,
        customData2:蓝
    }
}
$ stateProvider
  .STATE(联系人)
  .STATE('contacts.list',{
    templateUrl:contacts.list.html',
    数据:{
        customData1:44,
        customData2:红
    }
  })

使用上面的例子指出可以访问这样的数据:

 功能Ctrl键($州){
    的console.log($ state.current.data.customData1)//输出5;
    的console.log($ state.current.data.customData2)//输出蓝;
}


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>

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,
  ...

But I can't figure out the correct syntax to use access the value of level inside the displayName.

解决方案

The static data (suggested to be placed inside of data : {} - not to be directly on the root state settings) - are static data. So:

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...)

See the doc:

Attach Custom Data to State Objects

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屋!

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