I am comparing a variable with an array: $scope.object.id and $scope.groepen.id with an if statement after using the for loop. If $scope.object.id exactly matches one of the identifiers $scope.groepen.id , then it should make this index $scope.overlap true.
I use another if it checks if $scope.overlap is true. If one element of $scope.overlap is true, it will make $scope.bestaand true. Otherwise, it should make it false.
for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; if ($scope.overlap[i]) { $scope.bestaand = true; } else { $scope.bestaand = false; } } else { $scope.overlap[i] = false; } }
My console log shows me that $scope.overlap really shows the correct values (therefore, if nothing is the same, all indexes are false). $scope.bestaand returns true if something is the same, but does not return to false.
I use Angular form validation to show if validation works or not shown here:
<div class="col-md-3" ng-class="{ 'has-error' : bestaand }"> <label class="control-label" for="textinput">Groepsnaam</label> <input id="groepen" name="groepen" type="text" class="form-control input-md" ng-model="object.id" ng-minlength="4" ng-maxlength="16" ng-change="checkOverlap()" required> <p ng-show="bestaand" class="help-block">Deze groepsnaam bestaat al!</p> </div>
What am I doing wrong here?
Edit:
I changed the place of my if statements. Updated code shown here:
for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; } else { $scope.overlap[i] = false; } if ($scope.overlap[i]) { $scope.bestaand = true; console.log("works") } else { $scope.bestaand = false; console.log("doesnt work") } }
The console log shows me the following:

It seems like it is becoming true, but it is being overwritten (I have a value that matches the second value of the array). If I enter a value that matches the LAST value of the array, it works.