user1876246
user1876246

Reputation: 1229

stop an if statement javascript AngularJS

I have an if statement but need it to "break" if the first condition is met but AFTER I have completed some processes. The if statement is wrapped inside a forEach statement in Angular.

angular.forEach($scope.obj, function ( value, key) {
  if( id == key) {
    delete $scope.obj[id];
    result.clicked = false;
    //break here, don't run the else
  } else {
    $scope.obj[id] = result;
    result.clicked = true;
  }
})

Upvotes: 0

Views: 4498

Answers (3)

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

There's no way to do this, Angular forEach loop can't break on condition match. Use native FOR loop instead of angular.forEach, Because for will allow you to break in between.

Upvotes: 1

Pablo Lozano
Pablo Lozano

Reputation: 10342

Angular documentation says nothing about stopping forEach, it seems it cannot be stopped. Maybe you should use something like Array.prototype.every. On the other hand, maybe you can stop it throwing an error... but it seems a dirty way to do it.

Upvotes: 1

Spiked
Spiked

Reputation: 11

Since Angular.forEach is async, you can not break, because there is no loop. If you use a normal loop, you can break as usual.

Upvotes: 0

Related Questions