简体   繁体   English

使用AngularJS和ng-if检查用户是否存在

[英]Checking if user exists using AngularJS and ng-if

I am building a cross-platform app using Onsen UI, Monaca and AngularJS. 我正在使用Onsen UI,Monaca和AngularJS构建跨平台应用程序。 I need to check if a user exists based on the UserID they enter. 我需要根据他们输入的UserID检查用户是否存在。 If the UserID is found, then the name is returned to me, else the user wont be able to proceed to the next screen. 如果找到了UserID,则将名称返回给我,否则用户将无法继续下一个屏幕。

I send the UserID via API call ($http) and get a JSON object as a response. 我通过API调用($ http)发送UserID并获得一个JSON对象作为响应。 When I enter a UserID I can retrieve the JSON object (tested and working) but I am stuck on authentication. 输入UserID时,我可以检索JSON对象(经过测试并可以正常工作),但是我坚持进行身份验证。

When the user clicks the LOG IN button, I show a modal (ons-modal) window with a message of the users name if the user is found, but I am stuck on showing the message LOGIN FAIL if no user is found. 当用户单击“登录”按钮时,如果找到该用户,我将显示一个模态(基于模式)窗口,其中包含用户名的消息,但是如果找不到用户,我将停留在显示“登录失败”消息。

Is there perhaps a better way to handle this rather than a modal window? 是否有比模式窗口更好的方法来处理此问题?

Below is my code. 下面是我的代码。 And below is an example of the JSON that is returned. 以下是返回的JSON的示例。

[{"employee_name":"John Doe"}]

There is also a Controller that handles the API calls and it can be assumed this is working OK as I am able to get user names - but just stuck on authentication. 还有一个控制器可以处理API调用,可以假定这可以正常工作,因为我可以获取用户名-但只是停留在身份验证上。

login.html login.html

<div style="text-align: center; color: #889DA8">
    Enter your<br><strong>User ID</strong>
</div>

<form class="login-form" style="text-align: center" name="myForm">
    <section style="padding: 8px">
        <input type="password" class="text-input--underbar" required minlength="3" maxlength="4" ng-model-options="{ debounce : 800 }" placeholder="User ID" ng-model="userIDSearch" >
    </section>

    <section style="padding: 0 8px 8px">
        <ons-button var="saveBtn" ng-disabled="myForm.$invalid" modifier="large" onclick="modal.show('modal')">Log In</ons-button>
    </section>
</form>

<!-- Displays Modal Login Confirmation Screen -->
<ons-modal var="modal">
    <div class="alert-dialog-mask"></div>

    <div class="alert-dialog alert-dialog--android">
        <div class="alert-dialog-title alert-dialog-title--android">
            <div style="text-align: center">Please confirm your name</div>
        </div>

        <div class="alert-dialog-content alert-dialog-content--android">
            <div style="text-align: center; padding-top: 10px; padding-bottom: 15px; padding-left: 10px; padding-right: 10px;">
                <p ng-repeat="userID in userIDs">

                    <!-- If returned value != 0 then we have a valid User -->
                    <!-- NOT WORKING HERE -->
                    <span ng-if="userID.employee_name !== 0 ">
                        <strong>
                            {{userID.employee_name}}
                        </strong>    
                    </span>
                    <!-- NOT WORKING HERE -->        
                    <span ng-else>
                        <strong>
                            User ID not found. Please try again.
                        </strong>    
                    </span>                           
                </p>
            </div>
        </div>

        <div class="alert-dialog-footer alert-dialog-footer--one">
            <button class="alert-dialog-button alert-dialog-button--one" ng-click="modal.hide()">Cancel</button>
            <button class="alert-dialog-button alert-dialog-button--primal alert-dialog-button--one" ng-click="myNavigator.pushPage('vehicle-id.html', {animation: 'slide'});" )>Ok</button>
        </div>
    </div>
</ons-modal>

Would this be better handled in the app-controller perhaps? 也许可以在应用程序控制器中更好地处理此问题?

appController.js appController.js

// App Controller Start
angular.module("myApp", ['onsen']).controller("appController", function($scope, $http)
{
    // Watch for changes in the User ID text field
    $scope.$watch('userIDSearch', function()
    {
       fetchUSerID(); 
    });

    // Initialising the search field
    $scope.userIDSearch = "";

    function fetchUSerID()
    {
        $http.get("mymadeupdomain/api/login.php?userid=" + $scope.search).success(function(data)
        {
            $scope.userIDs = data;
        }); 
    }
});

Based on what I got from your question: 根据我从您的问题中得到的信息:

You can try initializing a $scope variable as $scope.result = false; 您可以尝试将$ scope变量初始化为$scope.result = false; In the success loop of $http.get() (ie if you get the user data), you can set it to true. 在$ http.get()的成功循环中(即,如果您获得用户数据),可以将其设置为true。

On Your modal window, you can use a condition like 在模态窗口中,您可以使用以下条件

<span ng-if = 'result'><{{bind user name variable here}}</span>
<span ng-if ='!result'>No User Found/ Login Failed</span>

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

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