Articles → ANGULAR.JS → angular.foreach in angular.js

angular.foreach in angular.js






Purpose





Syntax


angular.forEach(collection, function(value) {

});





Example


<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta charset="utf-8" />
	<title></title>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
	<script type="text/javascript">
	var app = angular.module('app', []);
	app.controller('myController', function($scope) {
		$scope.MyObject = [];
		var fruits = ["Apple", "Mango", "Banana", "Orange"];
		angular.forEach(fruits, function(value) {
			$scope.MyObject.push(value);
		});
	});
	</script>
</head>

<body>
	<div ng-app="app" ng-controller="myController">
		<div ng-repeat="fruit in MyObject"> {{fruit}} </div>
	</div>
</body>

</html>



Output


Picture showing the output of angular.foreach

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, December 31, 2016

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250