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!
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.
- 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.
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.
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>
{{country.1961}}
works fine for me check this JSFiddle,
but its better you can use bracket notation instead of dot notation when
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>
You can use backet notation {{country['1961']}}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With