Monday, 30 April 2018

023 AngularJS angular.forEach

AngularJS angular.forEach

AngularJS has inbuilt iterator to iterator through object collcetion .This functionality works both on object collection and array.'angular.forEach' directive is used for this purpose.

Below is the example of iterator on Array.

Example







<!DOCTYPE >
<head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>

<script>
 
           var app = angular.module("myApp", []);
                    app.controller("myController", function ($scope) 
           {

                       $scope.arr = ['Student 1', 'Student 2', 'Student 3', 'Student 4'];
                       $scope.idx = [];

                        angular.forEach($scope.arr, function (value, index) 
             {
                           $scope.idx.push(value);
                       });

});

</script>
<div style="background-color: #F0F0F0; width: 400px;">
        <div ng-app="myApp">
            <div ng-controller="myController" >
               <span ng-repeat="obj in idx">{{obj}}<br/></span>
           </div>
         </div>
</div>

Output :

Student 1Student 2Student 3Student 4

Below is the example of iterator on object collection.


Example



<!DOCTYPE >
<head>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>

<script>
var app = angular.module("myApp", []);
       app.controller("myController", function ($scope)
      {

            var values = [{ name: 'Student 1', gender: 'male' }, { name: 'Student 2', gender: 'male' }, { name: 'Student 3', gender: 'male'}];

            $scope.idx = [];

angular.forEach(values, function (value, key)
{

$scope.idx.push(value.name);
});

});

</script>
<div style="background-color: #F0F0F0; width: 400px;">
<div ng-app="myApp">
<div ng-controller="myController" >
<span ng-repeat="obj in idx">{{obj}}<br/></span>
</div>
</div>
</div>


Output :

Student 1Student 2Student 3

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...