1future
1future

Reputation: 51

How do i access the data in json object in Angularjs

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

Upvotes: 0

Views: 56

Answers (2)

sma
sma

Reputation: 9597

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>

Upvotes: 1

stan
stan

Reputation: 4995

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>

Upvotes: 0

Related Questions