Reputation: 2055
I want to fetch the list of friends for the REST-API into table using $http.get method in angularjs. Please see the DEMO. I am not able to load the JSON data, When I click on button.
JSON.data
{
"friends": [
{
"FirstName": "John",
"LastName": "Doe"
},
{
"FirstName": "Ann",
"LastName": "Wellington"
},
{
"FirstName": "Sabrina",
"LastName": "Burke"
}
]
}
Index.html
<body ng-app="step4App">
<div ng-controller="FriendsCtrl">
<button ng-click="loadFriends()">Load Friends</button>
<table>
<thead>
<tr><th>First</th><th>Last</th></tr>
</thead>
<tbody>
<tr ng-repeat="friend in friends">
<td>{{friend.FirstName}}</td>
<td>{{friend.LastName}}</td>
</tr>
</tbody>
</table>
</div>
<script>
var app=angular.module("step4App",[]);
app.controller("FriendsCtrl", function($scope, $http){
$scope.loadFriends=function(){
$http.get("friendsList.json").success(function(data){
$scope.friends=data;
}).error(function(){
alert("An unexpected error occured!");
});
}
});
</script>
</body>
Upvotes: 0
Views: 1031
Reputation: 1857
Your JSON file have an object, and $scope.friends
is an array.
You need change this:
$scope.friends=data;
to:
$scope.friends = data.friends;
Upvotes: 2