Using ForEach loop in AngularJS

rani
 
on Jul 07, 2020 11:29 PM
Sample_238539.zip
896 Views

How to use angular ForEach loop in AngularJS

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 08, 2020 03:57 AM

Hi rani,

Check this example. 

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope) {
            var customers = [
                    { CustomerId: 1, Name: "John Hammond" },
                    { CustomerId: 2, Name: "Mudassar Khan" },
                    { CustomerId: 3, Name: "Suzanne Mathews" },
                    { CustomerId: 4, Name: "Robert Schidner"}];

            $scope.Customers = [];
            angular.forEach(customers, function (customer, index) {
                if (customer.CustomerId == 2 || index == 2) {
                    $scope.Customers.push(customer);
                }
            });
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <table>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
            <tr ng-repeat="customer in Customers">
                <td>{{customer.CustomerId}}</td>
                <td>{{customer.Name}}</td>
            </tr>
        </table>
    </div>
</body>
</html>

Demo