Reputation: 1665
I get a json string from an $http
request, and I want to use ng-repeat
to display the data. As it stands, I get the json string from the server, but ng-repeat is not putting the data on the dom.
<div ng-controller="mycontroller2">
<form ng-submit="submit()">
{% csrf_token %}
Search:<input ng-model="artiste" />
<input type="submit" value="submit" />
</form>
<table ng-repeat="artist in artists">
<tr>
<td> {({ artist.fields.link })} </td>
</tr>
</table>
</div>
<script>
artApp.controller('mycontroller2', ['$scope', '$http',
function($scope, $http){
$scope.submit = function(){
var call = {
method: 'POST',
url: '/rest/',
data: {
"artiste": $scope.artiste
},
headers: {
'Content-Type': 'application/json',
'X-CSRFTOKEN': "{{ csrf_token }}"
}
};
$http(call)
.success(function(data){
$scope.artists = data;
})
}
}]);
The response data that shows up in devtools is a json string
"[{\"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \"https://trove2.storage.googleapis.com/leonardo-da-vinci/galloping-rider-and-other-figures.jpg\", \"title\": \"Galloping Rider and other figures\"}, \"model\": \"serve.art\", \"pk\": 63057},
I thought I could just iterate over it with ng-repeat
and generate html elements with the data, but it doesn't work right now.
Upvotes: 0
Views: 2051
Reputation: 1006
For Json Parsing in angular you have to use the following code in success method.
.success(function(data){
var response=angular.fromJson(data);
$scope.artists = response.fields;
})
modify your <table>
as below, Because you have assign the response array to $scope.artists.
Then you can use your json data with individual key.
<table ng-repeat="artist in artists">
<tr>
<td>{{artist.link}}</td>
</tr>
</table>
check whether the value is getting or not in console.
.success(function(data){
var response=angular.fromJson(data);
$scope.artists = response.fields;
console.log("artist :"+$scope.artists.artist);
console.log("link :"+$scope.artists.link);
})
Upvotes: 1
Reputation: 5233
You have an error in your html, try :
<td> {({ fields.artist.link.})} </td>
instead of artist.fields.link, according to your json : \"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \
Upvotes: 0