簡體   English   中英

如果語句在其他語句上再也沒有執行,則Angular JS應用程序始終會命中

[英]Angular JS Application Always Hit If Statement Never Goes on Else Statement

我正在將Wcf Service消費到Angular JS應用程序中。 我通過提供用戶名和密碼來創建用戶登錄系統。 但是,無論我在輸入字段中輸入的用戶名或密碼是什么,其始終顯示的消息用戶名和密碼都是正確的。 我想如果用戶名和密碼正確,那么我想在angular js應用程序中顯示消息,否則用戶名和密碼不正確,但是我不知道為什么它總是在if語句上起作用,並且如果我輸入錯誤的用戶名或密碼也沒關系。

這是我的腳本代碼。

  ///// <reference path="../angular.min.js" />  



var app = angular.module("WebClientModule", [])

    .controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {

        $scope.OperType = 1;

        //1 Mean New Entry  

        //To Clear all input controls.  
        function ClearModels() {
            $scope.OperType = 1;
            $scope.Username = "";
            $scope.Password = "";

        }
        $scope.login = function () {
            var User = {
                Username: $scope.Username,
                Password: $scope.Password,
            };
            myService.AuthenticateUser(User).then(function (pl) {

                if (pl.data) {
                    $scope.msg = "Password or username is correct !";//Always hit on this line
                }
                else {
                    $scope.msg = "Password Incorrect !";
                    console.log("Some error Occured" + err);
                }
                }, function (err) {
                $scope.msg = "Password or username is Incorrect !";
                console.log("Some error Occured" + err);
            });
        };



    }]);



app.service("myService", function ($http) {



    this.AuthenticateUser = function (User) {
        return $http.post("http://localhost:52098/HalifaxIISService.svc/AuthenticateUser", JSON.stringify(User));
    }
})

這是我的HTML代碼..

@{
    Layout = null;
}

<!DOCTYPE html>

<html ng-app="WebClientModule">
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/angular.min.js"></script>

    <script src="~/RegistrationScript/LoginScript.js"></script>
</head>
<body>

    <table id="tblContainer" data-ng-controller="Web_Client_Controller">
        <tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <div style="color: red;">{{msg}}</div>
                <table style="border: solid 4px Red; padding: 2px;">

                    <tr>
                        <td></td>
                        <td>
                            <span>Username</span>
                        </td>
                        <td>
                            <input type="text" id="username" data-ng-model="Username" required="" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <span>Password</span>
                        </td>
                        <td>
                            <input type="password" id="password" required data-ng-model="Password" require="" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <input type="button" id="Login" value="Login" data-ng-click="login()" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
</html>
<script src="~/RegistrationScript/LoginScript.js"></script>

這是輸出..

點擊這里查看結果

如果您將響應發布到您的http POST請求,這將有所幫助。 我敢打賭,即使使用錯誤的登錄信息,您仍然會發出有效的http請求,並因此獲得帶有數據(pl.data)的有效http響應,該數據可能表示登錄信息不正確。

如果pl.data為null或未定義,則只會命中else語句。 研究javascript條件以及它們如何與對象一起使用。 如果對象存在(不是null或未定義),則將其視為true。

在then()塊中,首先嘗試使用下面的代碼console.log(pl.data)檢查pl.data的值。 這樣,您將了解WCF服務返回的值。 如果服務始終返回True,那么在Angular中,它將始終執行if()語句。 例如

myService.AuthenticateUser(User).then(function (pl) {
console.log(pl.data)
if(pl.data){
    $scope.msg = "Password or username is correct !"
}
else{
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM