2017-05-01 8 views
0

こんにちは私はAngular JSフォームが入力に入力されたときに誰かのログイン情報が正しいことを確認できるようにしたいと思います。私はこの権利をしていますか?Angular JSがログイン情報を確認する

HERE'S THE ANGULAR JS 
var app= angular.module('multiPageApp', []); 


//SET UP: Array of users 

app.controller('logIn', function($scope){ 
    $scope.users = [ 
     { 
     username: 'Regie', 
     password: 'Tano' 
     }, 
     { 
     username: 'Jacob', 
     password: 'Minshall' 
     }, 
     { 
     username: 'Greg', 
     password: 'Mayer' 
     } 
    ] 

    $scope.log = function(){ 
    //Get Values of Inputs for username and password 
    var username= document.getElementById('input').value 
    var password = document.getElementById('password').value 

    //Loop through users to check if username and password are correct. 
    for(i = 0; i < users.length; i++){ 
     if(username == users[i].username && password == users[i].password){ 
     console.log('it worked!') 
     } 
    } 
    } 
}); 

答えて

0

最初に、入力フィールドとパスワードフィールドを取得するために、angle $ scope変数を使用してください。あなたがJavaScriptのフィルタを使用する(またはあなたがECMA6使用している場合も見つける)ために試合を取得する方法をすることができ、その後

<input type="text" id="input" ng-model="username" placeholder="Type your username..." /> 

if (users.filter(checkCredentials).length > 0) { 
    // ok case. 
} 
else { 
    // error case. Use ngMessages to show errors in the form 
} 

function checkCredentials(element) { 
    return element.username === $scope.username && element.password === $scope.password; 
} 
を あなたはそれらを以下のように、あなたの入力に関連付けられて定義することができますエラー検証を表示するには、角度で

ベストな方法は、使用してngMessagesです:

https://docs.angularjs.org/api/ngMessages/directive/ngMessages

0

あなたは、cにNG-モデルを使用する必要があります

<input type="text" id="username" ng-model="username" placeholder="Enter username" /> 
<input type="password" id="password" ng-model="password" placeholder="Enter password" /> 

を、このようにあなたのコントローラのチェックに:次のようにテキストボックスに入力された値をAptureの

VARアプリ= angular.module( 'multiPageApp'、[]);

//SET UP: Array of users 

app.controller('logIn', function($scope){ 
    $scope.users = [ 
     { 
     username: 'Regie', 
     password: 'Tano' 
     }, 
     { 
     username: 'Jacob', 
     password: 'Minshall' 
     }, 
     { 
     username: 'Greg', 
     password: 'Mayer' 
     } 
    ] 

    $scope.log = function(){ 

    //Loop through users to check if username and password are correct. 
    for(i = 0; i < users.length; i++){ 
     if(username == users[i].username && password == users[i].password){ 
     console.log('it worked!') 
     } 
    } 
    } 
}); 
関連する問題