
April 10, 2025 13:23 by
Peter
Directives, which are really nothing more than an extension of HTML attributes, are notions that AngularJS offers. The directives ng-init, ng-app, and ng-repeat are a few of them. This post will go over the idea of ng-repeat. In essence, the ng-repeat article is used to tie an array or list of data to HTML controls. For instance, we wish to connect a list of employees into an HTML table or div. Then we can use the ng-repeat directive to do this. So let's start by creating an angular controller and add some data in list of Employees. So our controller will look like the following.

var app = angular.module("mainApp", []);
app.controller('ngRepeatController', function ($scope, $http) {
$scope.EmpList = [];
$scope.EmpList.push({ Id: 1, Name: 'User A' });
$scope.EmpList.push({ Id: 2, Name: 'User B' });
$scope.EmpList.push({ Id: 3, Name: 'User C' });
$scope.EmpList.push({ Id: 4, Name: 'User D' });
});
Next, we bind the data to a html table, using the ng-repeat directive. For this, we first bind the controller to a div. and then use the ng-repeat directive on table element. So the code will look like the following.
<div ng-app="mainApp" data-ng-controller="ngRepeatController">
<table border="1" style="width: 200px">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="Emp in EmpList">
<td>{{ Emp.Id }}</td>
<td>{{ Emp.Name }}</td>
</tr>
</tbody>
</table>
</div>
Here we have created a simple html table and applied the ng-repeat on the tr tag of the table. This acts as a for-each loop on the rows and the rows get generated, for the number of records in the EmpList array. That's it, run the code and see the results.

And we are done...Happy coding!