angular-foreach-2

 

In this example of angular.forEach, Push will add values from ['A', 'B', 'C', 'D'] array to val array and index of ['A', 'B', 'C', 'D'] array to idx array and that will be displayed in the UI by the help of ng-repeat. 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"> <p ng-repeat="ix in idx">{{ix}}</p> <p ng-repeat="vl in val">{{vl}}</p> </div> </body> </html> <script> var app = angular.module("app", []); app.controller('forEachController', ['$scope', function ($scope) { $scope.idx = []; $scope.val = []; angular.forEach(['A', 'B', 'C', 'D'], function (value, index) { $scope.idx.push(index); $scope.val.push(value); }); }]); </script>
Output