0

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.

2
  • why using 3 braces..it should be 2 curly braces..so change it as <td> {{ artist.fields.link }} </td> ..then try? Commented Dec 4, 2014 at 12:23
  • This is a Django template. Two curly braces is the Django variable syntax. For angular I have to change the variable syntax to {({ var })} Commented Dec 4, 2014 at 19:35

3 Answers 3

1

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);
 })
Sign up to request clarification or add additional context in comments.

3 Comments

ng-repeat is still not showing anything on the dom.
I can see in the breakpoints that the vars data and response.fields have the json data that I want. It's just not making it over to ng-repeat
I believe the ng-repeat loop is showing a TypeError for the data it's iterating over.
0

Shouldn't JSON.parse just work?

$scope.artists = JSON.parse(data);

2 Comments

That works but the data is not making it to ng-repeat for printing to the dom.
I'm having a similar problem. My code works when saving as .html and just running it in the browser, but not when serving it from a simple node.js app. My guess it has something to do with content being served as the wrong type. ng-repeat loops, but doesn't print. Maybe you should be looking in that direction?
0

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\": \

1 Comment

That wasn't going to work because my ng-repeat = "artist in artists" used artist as the iterator, I could change it to entry in artists and it still wasn't working. However, the code works now after going away for a few hours, coming back, changing nothing, and trying it again. This keeps happening to me. I don't know if it's something with my browser, but my javascript won't work, then a few hours later it magically works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.