In this tutorial i am going to tell you about ng-repeat
directory to display your array data in a table.
As in programming, you iterate over the collection of objects in same way you will use ng-repeat directive in angularjs. using ng-repeat directive you can repeat your html elements.
ng-repeat
works on array of objects and iterate over object properties.
- <div ng-repeat="(key, value) in arrayObject"> ... </div>
You can use track by $index
if you think there is possibilty of duplicate data.
- <div ng-repeat="n in [1, 1, 2, 2] track by $index">
- {{n}}
- </div>
- <div ng-app="myApp" ng-controller="productCtrl">
- <table>
- <tr>
- <th>Name</th>
- <th>Details</th>
- </tr>
- <tr ng-repeat="x in products">
- <td>{{ x.name }}</td>
- <td>{{ x.details }}</td>
- </tr>
- </table>
- </div>
- angular.module('myApp', ['ngAnimate']).controller('productCtrl', function($scope) {
- $scope.products = [
- {name:'expertphp', details:'Best PHP Tutorial and articles available'},
- {name:'demo expertphp',details:'demo of each post'}
- ];
- });
You can access your json data from server to display in table by using $http
service.
- var app = angular.module('myApp', []);
- app.controller('productCtrl', function($scope, $http) {
- $http.get("http://www.expertphp.in/category/php")
- .then(function (response) {$scope.products = response.data.products;});
- });