AngularJs Break Foreach Loop


AngularJs Break Foreach Loop : Sometimes while working with Angularjs Foreach loop we need to break loop, there is no direct method to add break in Angular foreach loop. Here in this tutorial we are going to explain how you can use do this in foreach loop. You can also use our online editor to edit and run the code online.


AngularJs Break Foreach Loop Example

There is no break statement in Angular foreach loop, still you can do this something like this –

AngularJs Break Foreach Loop Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.html= '';
 var myArray = [];
 myArray['99'] = 'John';
 myArray['100'] = 'Kelly';
 myArray['490'] = 'Ryan';
  $scope.foreachExample = function() {
  angular.forEach(myArray, function(value, key) {
  if(key == "100"){
   return false;
  }
  $scope.html += "Id : "+key+", Name: "+value+" | ";
  });
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    Result = {{html}}
	<br>
    <button ng-click ="foreachExample()">foreach Example</button>
	<br>
	
</div> 
</div>
</body>  
</html>                            

Try it »

If you run the above example it will produce output something like this –

AngularJs Break Foreach Loop Example


Advertisements

Add Comment

📖 Read More