Angular.js – using angular.forEach() function

A simple Angular.js example of using a forEach function from AngularJS API.
<!DOCTYPE html>
<html ng-app="testApp">
<head>

<style>

</style>

    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Angular.js example"/>

    <title>Tizen Angular.js example</title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>

    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/angular.min.js"></script>
    <script>

        var app = angular.module('testApp', []);

        app.controller('AppController', function($scope) {

          var namesArray = { name1:'John', name2:'Greg', name3:'Mary', name4:'Joanna', name5:'Chloe', name6:'Anna' };
          var fullPhrases = [];

            $scope.clicked = false;

            $scope.change = function() {

                $scope.clicked = true;

                angular.forEach(namesArray, function(value, key) {
                  fullPhrases.push('The ' + key + ' value is ' + value + '.');
                });

              $scope.string1 = namesArray;
              $scope.string2 = fullPhrases;

            }

        });

    </script>
</head>
<body>
  <div ng-controller="AppController">
  <br>
    <button id="myButton" ng-click="change()" ng-hide="clicked">
      Click this button to uppercase the string below
    </button>
    <br><br>
    the names array: <b>{{ string1 }}</b>
    <br><br>
    after changing with the for each iterator function: <b>{{ string2 }}</b>
  </div>
</body>

</html>

Responses

0 Replies