0

I am trying to access a the data in a json object using angular but i finding it difficult to this at the moment and hopefully you can help me here.

so i have a function in my controller like this:

var vm = this;
vm.getData = getData;
vm.data = [];


 function getData() {
        var promise = appService.getAll(vm.query);
        promise.then(function(response) {
            vm.data = response.data,
                    console.log(vm.data);
            },
            function(error) {
                $log.error("an error here", error);
            });


    }

and my view goes something like this:

<div> 


<table class="table">
        <tr >
            <th> Department</th>


        </tr >
        <tr ng-repeat="n in vm.data">
            <td>{{n.sectionName}} </td>
        </tr>


    </table>


</div>

Everything works .. i retrieve my json object as required .. but its just accessing them is where i am having in issue as the above context in my view is not dispplayed .. so in my example above i am trying to acess a json with the heaser of "sectionName" and displaying it in my view.

here is a visual of of the json object in my console ... enter image description here

Thank you for your time

2
  • if the response object as in the image have results as the data array, then why you call response.data, shouldn't it be response.results? Commented Dec 16, 2015 at 15:26
  • yup .. its exactly that! Commented Dec 16, 2015 at 15:49

2 Answers 2

1

If results is the data you need to display, then you should adjust your markup to be this:

<tr ng-repeat="n in vm.data.results">
    <td>{{n.sectionName}} </td>
</tr>
Sign up to request clarification or add additional context in comments.

Comments

0

You are looping over the object itself. Instead, you have to loop over the results array inside the object.

<div> 


<table class="table">
        <tr >
            <th> Department</th>


        </tr >
        <tr ng-repeat="n in vm.data.results">
            <td>{{n.sectionName}} </td>
        </tr>


    </table>


</div>

1 Comment

yup thats right .. it was vm.data.response.results.. thank you

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.