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">
 <script src="//"></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+" | ";
<div ng-app="myApp">  
<div ng-controller="myController">  
    Result = {{html}}
    <button ng-click ="foreachExample()">foreach Example</button>

Try it »

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

AngularJs Break Foreach Loop Example


Add Comment

📖 Read More