简体   繁体   English

angular-Js:无法访问同一控制器内不同函数调用中的数组元素

[英]angular-Js: not able to access array elements in different function call within same controller

I just need to store data in $scope.user on register() call, and trying to login using its data elements.. But not successful. 我只需要在register()调用$scope.user数据存储在$scope.user ,并尝试使用其数据元素登录。 I've used simplest way, I think, there may be other ways(like localStorage, cookies etc.) Im new to angular, don't know how to implement them. 我认为我使用了最简单的方法,可能还有其他方法(例如localStorage,Cookie等)。我对angular还是陌生的,不知道如何实现它们。 Please help me out... 请帮帮我...

Controller : 控制器:

var app = angular.module("app",[]);     
    app.controller("ctr",function($scope){    

         $scope.user = [{
                        fname:"",
                        lname: "",
                        username: "",
                        password: ""
                        }];  

          $scope.register = function(userData){
              $scope.user.push(userData);
              alert($scope.user.username); *//show undefined*       
           };

          $scope.login = function(loginData){
             if(loginData.username == $scope.user.username) {
               alert("Login successfully!!");
              }
              else alert("Wrong user!");
           };
        });

View : 查看:

<body ng-app="app">
    <div ng-controller="ctr">       
        <table >
             <tr>
                 <td><label>Username : </label></td>
                 <td><input type="text" ng-model="loginData.username" placeholder="Name" /></td>                 
             </tr>
             <tr>
                 <td><label>Password : </label></td>
                 <td><input type="password" ng-model="loginData.password" placeholder="Password" /></td>                 
            </tr>
            <tr>                
                <td colspan="2">
                    <input type="button" value="Login" ng-click="login(loginData)" class="save"/>
                </td>               
            </tr>
        </table>

        <table >
            <tr>
                 <td><label>First Name : </label></td>
                 <td><input type="text" ng-model="userData.fname" placeholder="First Name" /></td>               
             </tr>
             <tr>
                 <td><label>Last Name : </label></td>
                 <td><input type="text" ng-model="userData.lname" placeholder="Last Name" /></td>                
            </tr>
             <tr>
                 <td><label>Username : </label></td>
                 <td><input type="text" ng-model="userData.username" placeholder="Name" /></td>              
             </tr>
             <tr>
                 <td><label>Password : </label></td>
                 <td><input type="password" ng-model="userData.password" placeholder="Password" /></td>              
            </tr>
            <tr>                
                <td colspan="2"><input type="button" value="Register" ng-click="register(userData)" class="save"/></td>                 
            </tr>               
        </table>        
    </div>    
</body>

Would really appreciate if someone could help me out with this problem. 如果有人可以帮助我解决这个问题,我们将不胜感激。

You push the userdata to an array, your $scope.user is an array, not an object. 您将userdata推送到一个数组, $scope.user是一个数组,而不是一个对象。 Try this instead: 尝试以下方法:

$scope.user = {};  

$scope.register = function(userData){
    $scope.user = userData;
    alert($scope.user.username);
};

由于变量$scope.user是一个数组,因此您应该使用如下代码:

alert($scope.user[0].username)

Your if statement is false: 您的if陈述式为假:

if(loginData.username == $scope.user.username) {

your $scope.user is an array and you should search first for the object with this username, before you can compare the password. 您的$scope.user是一个数组,应该先搜索具有该用户名的对象,然后才能比较密码。

$scope.login = function(loginData){
             //Filter your user array and find the user with given username
             var user = $filter('filter')($scope.user, function (u) {return u.username === loginData.username;})[0];
             //Compare login data
             if (user == null) {
                alert("User not found");
             } else {
                if(user.password == loginData.password) {
                   alert("User logged in");
                } else {
                   alert("Wrong Password");
                }
             }
};

I've got no possibility to test my answer, please give me a feedback. 我无法测试我的答案,请给我反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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