Skip to content
Related Articles
Open in App
Not now

Related Articles

AngularJS angular.forEach() Function

Improve Article
Save Article
Like Article
  • Last Updated : 14 Feb, 2023
Improve Article
Save Article
Like Article

The angular.forEach() Function in AngularJS is used to iterate through each item in an array or object. It works similar to the for loop and this loop contains all properties of an object in key-value pairs of an object. 

Syntax:

angular.forEach(object, iterator, [context])

Parameter Values:

  • object: It refers to the object to be iterated.
  • iterator: It refers to the iterator function(value, key, obj).
  • context: This is optional. It refers to the object which becomes the context for the iterator function.

 

Example: This example describes the basic usage of the forEach() function in AngularJS.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
    </script>
    <title>angular.forEach()</title>
</head>
  
<body ng-app="app" ng-cloak style="padding:30px">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>angular.forEach()</h2>
    <p>Searching techniques:</p>
    <div ng-controller="geek">
        <div ng-repeat="name in names">
            <ul>
                <li>{{name}}</li>
            </ul>
        </div>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope', function ($scope) {
            $scope.names = [];
            var values = [
                { name: 'Binary Search' },
                { name: 'Linear Search' },
                { name: 'Interpolation Search' }
            ];
            angular.forEach(values, function (value, key) {
                $scope.names.push(value.name);
            });
        }]);
    </script>
</body>
</html>


Output:

foreach


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!