0

No matter where I place the variable. The changes do not reflect outside the loop.

$scope.getSubsByEmail = function(email) {
    var userNotFound = 0;
    var arr = []
    SITES.forEach(function(site) {
        user_email_api.get({}, function(response) {
            if(response.success) {
                userNotFound -= 1;
            } else {
                userNotFound += 1;
                arr.push(1);
            }
        });
    });

    console.log(userNotFound); # Remains 0
    console.log(arr);          # Works
    if (userNotFound == 6) {
        flashAlert({success:false, errorMessage: "User Not Found"});
    }

I'm facing this problem everywhere and I can't figure out why it does not update the value. Only items pushed to an array are reflected but not the variable. Missing something basic about JS or Angular itself.

Ayush
  • 479
  • 2
  • 9
  • 24

0 Answers0