如何在具有数字作为关键字的JSON数据上使用ng-repeat?

时间:2015-09-04 09:49:25

标签: json angularjs

我想使用ng-repeat来显示数据表 提供像这样的JSON格式

{"name":"Aruba","code":"ABW","1960":54208,"1961":55435},
{"name":"Afghanistan","code":"AFG","1960":8774440,"1961":8953544}

但我的代码似乎不适用于{{country.1961}}。

<table>
      <tr>
        <td>Country name</td>
        <td>1960 population</td> 
        <td>1970 population</td>
        <td>1980 population</td> 
        <td>1990 population</td>
        <td>2000 population</td> 
        <td>2010 population</td>
        <td>Percentage growth between 1960 and 2010</td>
      </tr>
      <tr ng-repeat="country in countries">
        <td>{{ country.name }}</td>
        <td>{{ country.1961 }}</td>

      </tr>
    </table>

当我删除<td>{{ country.1961 }}</td>时,一切正常。 我该如何解决?

谢谢!

2 个答案:

答案 0 :(得分:4)

{{country.1961}}对我来说很好用,请查看 JSFiddle , 但是,当

时,你可以使用括号表示法而不是点符号
  • 键是完全数字,如'1985'
  • 键包含“我的地方”
  • 等空格

<强> JSFiddle

 <table border='1'>
        <tr>
            <td>Country name</td>
            <td>1960 population</td>
            <td>1970 population</td>
            <td>1980 population</td>
            <td>1990 population</td>
            <td>2000 population</td>
            <td>2010 population</td>
            <td>Percentage growth between 1960 and 2010</td>
        </tr>
        <tr ng-repeat="country in countries">
            <td>{{ country.name }}</td>
            <td>{{ country['1961'] }}</td>
        </tr>
    </table>

答案 1 :(得分:2)

您可以使用backet表示法{{country['1961']}}