AngularJS forEach example

AngularJS forEach example

Below is sample code to iterate values using AngularJS forEach where we have created key and value pair object and iterating them using forEach and store in temporary array.

<!DOCTYPE html>
<html data-ng-app="myapp">
<head>
<meta charset="ISO-8859-1">
<title>AngularJS Tutorial</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
    var app = angular.module('myapp', []);
 
    app.controller('TestController', function($scope){
        
    	var values = {Name: 'Java Honk', Location: 'NY', Date: '20141228', Phone: '123456789'};
        var tempStore = [];
        angular.forEach(values, function(value, key) {
        	tempStore.push(key + ': ' + value);
        });
        
        document.write("<h2>Variable values:</h2>");
        document.write(tempStore);
    });
</script>
</head>
<body data-ng-controller="TestController">
</body>
</html>
  • Output:

AngularJS forEach example

  • Test this code in Plunker here
  • For more information on AngularJS forEach please refer this link

Leave a Reply

Your email address will not be published. Required fields are marked *