Overview
Today, in this article, we are going to see how we can use the ng-repeat directive in Angular JS and also the nesting of the ng-repeat directive. Kindly refer to my previous article for more.
- Overview Of AngularJS
- Modules And Controller In AngularJS
- Controllers in AngularJS
- AngularJS ng src Directive
Let's start,
Introduction
Here, I want to display EmployeeName, Address, Phone number, and their respective salaries.
Suppose I want to display five records and the ng-repeat directive is similar to the for loop in C#. We will understand, that we need to build a model first and need to add an employee array. Thus, switch to VS.
I created an array called employees, given below.
var mypartone = angular.module("mymodule", []).controller("myController", function($scope) {
var employees = [];
});
Now, in it, we will add FirstName, Address, PhoneNumber, and Salaries.
Created objects employees
{ "firstName": "Akshay", "Address": "Mumbai", "PhoneNumber": "12345", "Salary": "1234" },
{ "firstName": "Hari", "Address": "Mumbai", "PhoneNumber": "1234533", "Salary": "12345" },
{ "firstName": "Milind", "Address": "Mumbai", "PhoneNumber": "1234556", "Salary": "123477" },
{ "firstName": "Raghavan", "Address": "Pune", "PhoneNumber": "69874", "Salary": "8888" },
{ "firstName": "Hari", "Address": "Mumbai", "PhoneNumber": "2525", "Salary": "5555" }
Thus, our final code is.
var mypartone = angular.module("mymodule", []).controller("myController", function($scope) {
var employees = [
{
firstName: "Akshay",
Address: "Mumbai",
PhoneNumber: "12345",
Salary: "1234"
},
{
firstName: "Hari",
Address: "Mumbai",
PhoneNumber: "1234533",
Salary: "12345"
},
{
firstName: "Milind",
Address: "Mumbai",
PhoneNumber: "1234556",
Salary: "123477"
},
{
firstName: "Raghavan",
Address: "Pune",
PhoneNumber: "69874",
Salary: "8888"
},
{
firstName: "Hari",
Address: "Mumbai",
PhoneNumber: "2525",
Salary: "5555"
}
];
$scope.employees = employees;
});
Now, we want to present this data in a tabular format.
In this table, I added a <thead> section and respective tr and td to display the details, given below.
<thead>
<tr>
<td>FirstName :</td>
<td>Address :</td>
<td>PhoneNumber :</td>
<td>Salary :</td>
</tr>
</thead>
I am going to add a <tbody> section and in this <tbody>, we will display the records.
In this <tbody> section, we will add a <tr> and <td> section.
In this <tr> section, we will add the ng-repeat directive as,
<tr ng-repeat="employee in employees">
Thus, read it as an employee in our model, which is called employees.
Hence, our <tbody> section code is,
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.firstName}}</td>
<td>{{employee.Address}}</td>
<td>{{employee.PhoneNumber}}</td>
<td>{{employee.Salary}}</td>
</tr>
</tbody>
The final ASPX page is given below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Angular.aspx.cs" Inherits="Angular_JS.Angular" %>
<!DOCTYPE html>
<html ng-app="mymodule">
<head runat="server">
<title>Overview Angular </title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/mytest.js"></script>
</head>
<body ng-controller="myController">
<form id="form1" runat="server">
<div>
<table>
<thead>
<tr>
<td>FirstName :</td>
<td>Address :</td>
<td>PhoneNumber :</td>
<td>Salary :</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.firstName}}</td>
<td>{{employee.Address}}</td>
<td>{{employee.PhoneNumber}}</td>
<td>{{employee.Salary}}</td>
</tr>
</tbody>
</table> <br /><br /><br /> </div>
</form>
</body>
</html>
Now, just run the solution and you will see the output.









Vignesh ManiPosted Jul 21, 2016, 4:45 PM
Nice
kalu singh raoPosted Jul 21, 2016, 1:50 AM
Useful Article
VenkatPosted Jul 20, 2016, 9:14 PM
Very informative article