0

I'm trying to databind an object received via an HTTP request to a table in angularjs.

The normal way to do this would be to use ng-repeat as follows.

 <table class="table table-striped">
        <tr>
            <th>name</th>
            <th>artist</th>
        </tr>
        <tr>
            <td ng-repeat="track in $scope.trackList.items">
                {{ track.name }}
                {{ track.artist }}
            </td>
        </tr>
    </table>

The problem with this is that the page loads and ng-repeat is ran before the data is returned from the server causing no items to be in the collection so nothing is drawn into the table.

What would be the best way to do this?

3 Answers 3

2

You don't need $scope in your view. Also, you probably want to put ng-repeat on <tr>, not <td>.

<table class="table table-striped">
    <tr>
        <th>name</th>
        <th>artist</th>
    </tr>
    <tr ng-repeat="track in trackList.items">
        <td>{{ track.name }}</td>
        <td>{{ track.artist }}</td>                
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need $scope here just simple trackList.items.

$scope is the glue b/w Views and controller and you don't need to explicitly call $scope in the view it is already implicit that things are already picking from the scope.

<td ng-repeat="track in trackList.items">
                {{ track.name }}
                {{ track.artist }}
            </td>

And if you are talking about the http call then here come's the magic of two way binding into picture.If anything update in controller by $http call that will be reflect in view as well you don't need to do it manually.

Hope it helps. :)

1 Comment

haha nice it worked inherently! I was looking to deep into it + the typo... Thanks!
0

Let me correct your code first

<table class="table table-striped">
    <tr>
        <th>name</th>
        <th>artist</th>
    </tr>
    <tr>
         <td ng-repeat="track in trackList.items">{{ track.name }}</td>
         <td>{{ track.artist }}</td>
        </tr>
</table>

You don't have to explicitly specify $scope while binding data.

With respect to your problem, even though the code will get executed when the page is getting loaded, any changes that you do to the scoped data will be honored by the angular and view will get updated.

In your case, at the time of page load, if there is data available in trackList.items it will be shown in the page. Otherwise table will be rendered with just headers. Later when the application receives data from AJAX (or any other source), you have to simply assign it to $scope.trackList in your JS code. This will result in instant view updates and you will see the table starts reflecting the new data.

Comments

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.