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

时间:2022-11-16 20:34:01

I want to use ng-repeat to display the table of data that provide by JSON format like this

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

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

But my code below seems doesn't work with {{ country.1961 }}.

但是我的代码似乎不适用于{{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>

When i delete <td>{{ country.1961 }}</td> everything works fine. How can i fix it?

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

Thanks!

2 个解决方案

#1


4  

{{country.1961}} works fine for me check this JSFiddle, but its better you can use bracket notation instead of dot notation when

{{country.1961}}对我来说很好用,请查看这个JSFiddle,但更好的是你可以使用括号表示而不是点符号

  • keys are fully numbers like '1985'
  • 钥匙是完全数字,如'1985'

  • keys contains spaces like 'My Place'
  • 键包含“我的地方”等空格

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>

#2


2  

You can use backet notation {{country['1961']}}.

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

#1


4  

{{country.1961}} works fine for me check this JSFiddle, but its better you can use bracket notation instead of dot notation when

{{country.1961}}对我来说很好用,请查看这个JSFiddle,但更好的是你可以使用括号表示而不是点符号

  • keys are fully numbers like '1985'
  • 钥匙是完全数字,如'1985'

  • keys contains spaces like 'My Place'
  • 键包含“我的地方”等空格

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>

#2


2  

You can use backet notation {{country['1961']}}.

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