Reputation: 133
I am trying to read data from json file into angular.js but i am not getting data.
[
{
"code": "AD",
"name": "Andorra",
"population": "84000"
},
{
"code": "TN",
"name": "Tunisia",
"population": "10589025"
},
{
"code": "TW",
"name": "Taiwan",
"population": "22894384"
},
{
"code": "TZ",
"name": "Tanzania",
"population": "41892895"
},
{
"code": "UA",
"name": "Ukraine",
"population": "45415596"
},
{
"code": "UG",
"name": "Uganda",
"population": "33398682"
},
{
"code": "UM",
"name": "U.S. Minor Outlying Islands",
"population": "0"
},
{
"code": "US",
"name": "United States",
"population": "310232863"
},
{
"code": "UY",
"name": "Uruguay",
"population": "3477000"
},
{
"code": "UZ",
"name": "Uzbekistan",
"population": "27865738"
},
{
"code": "VA",
"name": "Vatican City",
"population": "921"
},
{
"code": "VC",
"name": "Saint Vincent and the Grenadines",
"population": "104217"
},
{
"code": "VE",
"name": "Venezuela",
"population": "27223228"
},
{
"code": "VG",
"name": "British Virgin Islands",
"population": "21730"
},
{
"code": "VI",
"name": "U.S. Virgin Islands",
"population": "108708"
},
{
"code": "VN",
"name": "Vietnam",
"population": "89571130"
},
{
"code": "VU",
"name": "Vanuatu",
"population": "221552"
},
{
"code": "WF",
"name": "Wallis and Futuna",
"population": "16025"
},
{
"code": "WS",
"name": "Samoa",
"population": "192001"
},
{
"code": "ZW",
"name": "Zimbabwe",
"population": "11651858"
}
]
index.html
<html ng-app="countryApp">
<head>
<meta charset="utf-8">
<title>Angular.js JSON Fetching Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
var countryApp = angular.module('countryApp', []);
countryApp.controller('CountryCtrl', function ($scope, $http){
$http.get('countries.json').success(function(data) {
$scope.countries = data;
});
});
</script>
</head>
<body ng-controller="CountryCtrl">
<h2>Angular.js JSON Fetching Example</h2>
<table>
<tr>
<th>Code</th>
<th>Country</th>
<th>Population</th>
</tr>
<tr ng-repeat="country in countries">
<td>{{country.code}}</td>
<td>{{country.name}}</td>
<td>{{country.population}}</td>
</tr>
</table>
</body>
</html>
Upvotes: 0
Views: 237
Reputation: 1692
Please check the path of the json file because the code it's fine.
If you are executing the code from your local machine you need a server to run the code like nodejs.
Upvotes: 0
Reputation: 702
when you make an $http.get call, the callback function returns the response object. This means that you need to access the data property of the response, not just assign the response:
$http.get('countries.json').success(function(response) {
$scope.countries = response.data;
});
take a look at the docs for $http.get, specifically the example ($http docs)
Upvotes: 2