AngularJS - 显示键的值,即数字

时间:2017-04-12 09:47:10

标签: html angularjs

我检查了几个先前提出的问题,但找不到我要找的答案。我正在研究一个使用ng-repeat的表来完成我的案例任务中的数组。重复如下:

task {
    x : y,
    a : b,
    c : d,
    custom_fields {
       89829 : value
    }
}

我的任务对象如下所示,它由常规值和custom_fields对象组成:

task.custom_fields.89829

当我尝试使用以下方式显示密钥的值时:

task.custom_fields.test

它返回语法错误,因为该键是一个意外的令牌。当我将其重命名为不同的内容时,例如:

   var chart = AmCharts.makeChart("chartdiv", {

      "type": "serial",
      //"dataProvider": generateChartData(),
/*        "dataLoader": {
        "url": "getInstituteChartDataListDash.do"
      },  */
      "dataLoader": {
            "url": "getInstituteChartDataListDash.do",
            "format": "json"
          },
      "valueAxes": [{
        "gridColor": "#FFFFFF",
        "gridAlpha": 0.2,
        "dashLength": 0
      }],
      "gridAboveGraphs": true,
      "startDuration": 1,
      "graphs": [{
        "balloonText": "[[category]]: <b>[[value]]</b>",
        "fillAlphas": 0.8,
        "lineAlpha": 0.2,
        "type": "column",
        "valueField": "totalInstitute"
      }],

      "graphs": [{
            "balloonText": "[[category]]: <b>[[value]]</b>",
            "fillAlphas": 0.8,
            "lineAlpha": 0.2,
            "type": "column",
            "valueField": "totalMpoInstitute"
          }],


      "chartCursor": {
        "categoryBalloonEnabled": false,
        "cursorAlpha": 0,
        "zoomable": false
      },
      "categoryField": " institute",
      "categoryAxis": {
        "gridPosition": "start",
        "gridAlpha": 0,
        "tickPosition": "start",
        "tickLength": 20
      }
    });

它确实显示正确。

我的问题是,是否可以显示一个数字键的值?

1 个答案:

答案 0 :(得分:1)

在方括号内访问该属性

task.custom_fields['89829']

你的json也是无效的。像这样修改

var task = {
    x : y,
    a : b,
    c : d,
    custom_fields: {
       89829 : value
    }
}

演示

var task =  {
    x : 1,
    a : 2,
    c : 3,
    custom_fields : {
       89829 : 123
    }
}

console.log(task.custom_fields['89829'])