简体   繁体   中英

Use server-side JSON array objects as login details in AngularJS 1.4

I have a server-side JSON array which I can retrieve. However I have issues trying to use the objects of the array as login details.

$scope.loginvalidator = function(){
   var logins = [ 
        { 
        username: $scope.Users.LoginName,
        password: $scope.Users.Password,
        },
         { 
           username: '1',
           password: '1',
         },
          ];
 for(var i = 0; i<logins.length; i++) {
    if ($scope.userInput == logins[i].username &&
        $scope.pswInput == logins[i].password){
          $scope.feedback = 'Login Successful';
          return true;
    }
    else {$scope.feedback = 'Login Failed';}
  }
  };

My code is only recognizing the hardcoded words and not reading $scope.Users.LoginName/Password as the LoginName and Password kept in the JSON array. I have bound the file read to an ng-click which I run before trying to login.

It looks like the for loop is not stopped. Instead of 'return true', try 'break;'

for(var i = 0; i<logins.length; i++) {
  if ($scope.userInput == logins[i].username &&
    $scope.pswInput == logins[i].password){
      $scope.feedback = 'Login Successful';
      break; //change this line
    }
    else {$scope.feedback = 'Login Failed';}
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM