angular-foreach-1

 

angular.foreach works very similar to for loop and this loop contains all properties of object in key-value pairs of object. In this example, We have 'values' as an array object and angular.forEach iterate on each property of obj and push will add the name property in 'names' array and display name in UI. See the code snippet:

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome in the AngularJS</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> </head> <body ng-app="app"> <div ng-controller="forEachController"> <div ng-repeat="name in names"> {{name}} </div> </div> </body> </html> <script> var app = angular.module("app", []); app.controller('forEachController', ['$scope', function ($scope) { $scope.names = []; var values = [{name: 'Jimi', gender: 'male'},{name: 'Peter', gender: 'male'},{name: 'Bob', gender: 'male'}]; angular.forEach(values, function (value, key) { $scope.names.push(value.name); }); }]); </script>
Output