In AngularJS, managing and iterating over data collections is a common task. While JavaScript provides several ways to iterate through arrays and objects, two commonly used loops are while
and foreach
. This article will guide you through using these loops within an AngularJS application.
while Loop
The while
loop in JavaScript is used to execute a block of code as long as a specified condition is true. Here’s how you can use it in an AngularJS context:
Example. Using while
Loop in AngularJS
Consider a scenario where you need to process a list of numbers until a certain condition is met:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>While Loop in AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="myController">
<h1>While Loop Example</h1>
<p ng-repeat="number in processedNumbers">{{ number }}</p>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
var numbers = [1, 2, 3, 4, 5];
var index = 0;
$scope.processedNumbers = [];
while (index < numbers.length) {
if (numbers[index] % 2 === 0) {
$scope.processedNumbers.push(numbers[index] * 2);
} else {
$scope.processedNumbers.push(numbers[index]);
}
index++;
}
});
</script>
</body>
</html>
In this example, the while
loop iterates through an array of numbers and processes each number based on a condition. If the number is even, it multiplies it by 2; otherwise, it leaves it unchanged.
foreach Loop
The foreach
loop in JavaScript is used to execute a block of code for each element in an array. AngularJS provides its own angular.forEach
method, which is similar to the native forEach
method but offers some additional features.
Example. Using angular.forEach
in AngularJS
Here’s how you can use the angular.forEach
method to iterate over an array of objects:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>foreach Loop in AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="myController">
<h1>foreach Loop Example</h1>
<ul>
<li ng-repeat="person in processedPeople">{{ person.name }} - {{ person.age }}</li>
</ul>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
var people = [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 30 },
{ name: 'Jim', age: 35 }
];
$scope.processedPeople = [];
angular.forEach(people, function(person) {
if (person.age > 30) {
person.category = 'Senior';
} else {
person.category = 'Junior';
}
$scope.processedPeople.push(person);
});
});
</script>
</body>
</html>
In this example, the angular.forEach
method iterates through an array of people objects and adds a new property (category
) based on their age.
Conclusion
Both while
and foreach
loops are essential tools in AngularJS for processing and manipulating data collections. The while
loop is useful when you need to repeat a block of code based on a condition, whereas the foreach
loop (or angular.forEach
method) is ideal for iterating over arrays or objects.