Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use ng-repeat on a JSON data that have numbers as key of it?

Tags:

json

angularjs

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

{"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 }}.

<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?

Thanks!

like image 839
Varis Darasirikul Avatar asked Sep 04 '15 09:09

Varis Darasirikul


People also ask

How to use ng-repeat directive in AngularJS through JSON object?

The Json contains th key value pair. So it take the key i.e { { l.label }} and prints its value. We will see the functioning of the ng-repeat directive in angularJS through Links. The links are loads through the json object. It can also index the link for a perticular name.

How to iterate over the keys and values with ng-repeat in AngularJS?

- GeeksforGeeks How to iterate over the keys and values with ng-repeat in AngularJS ? The task is to iterate over a JS object (its keys and values) using the ng-repeat directive. This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS.

How to iterate through all the items of the customers JSON array?

The TBODY element of the HTML Table has been assigned ng-repeat directive in order to iterate through all the items of the Customers JSON array. For each JSON object in the Customers JSON array, a TR element (HTML Table Row) is generated and appended into the HTML Table.

How to ask for a key-value pair parameter from AngularJS?

This can be done using parenthesis in the ng-repeat directive to explicitly ask for a key-value pair parameter from angularJS. Here the variable key contains the key of the object and value contains the value of the object. <element ng-repeat=" (key, value) in JSObject"> Contents... </element>


2 Answers

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

  • keys are fully numbers like '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>
like image 177
Nidhish Krishnan Avatar answered Sep 18 '22 15:09

Nidhish Krishnan


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

like image 25
Eloims Avatar answered Sep 21 '22 15:09

Eloims