AngularJS: Bind JSON Data to HTML Table using ng-repeat

Introduction: In this article I am going to explain how to populate JSON data in HTML table using ng-repeat in AngularJS. This article also covers how to add serial number to each row and sort the table data.


Bind JSON data to HTML table using ng-repeat in AngularJS

Implementation: Let’s create a sample html page to bind json data to table.
<html>
<head>
    <title>Bind json data to HTML table using AngularJS</title>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <style>
        table, th, td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
            text-align: left;
        }

            table tr:nth-child(odd) {
                background-color: #f2f2f2;
            }

            table tr:nth-child(even) {
                background-color: #ffffff;
            }
    </style>
</head>
<body>
    <div ng-app="bookApp" ng-controller="bookController">
        <table border="0">
            <tr>
                <td>
                    <table>
                        <tr>
                            <th>Sr. No.</th>
                            <th>Book Title</th>
                            <th>Author</th>
                            <th>Price</th>
                        </tr>
                        <tr ng-repeat="book in bookList | orderBy : 'title'">
                            <td>{{$index +1}}</td>
                            <td>{{ book.title }}</td>
                            <td>{{ book.author }}</td>
                            <td>{{ book.price }}</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <script>
        var bookApp = angular.module("bookApp", []);
        bookApp.controller('bookController', function ($scope) {
            $scope.bookList = [
            { "title": "Hobbit", "author": "Aman", "price": 700 },
            { "title": "Lord of the rings", "author": "Sameer", "price": 1000 },
            { "title": "Harry Porter", "author": "Anuj", "price": 650 },
            { "title": "The little prince", "author": "Jatin", "price": 870 },
            { "title": "Angels and Demons", "author": "Shivam", "price": 890 }
            ];
        });
    </script>
</body>
</html>
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..