简体   繁体   English

如果语句在其他语句上再也没有执行,则Angular JS应用程序始终会命中

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

I am consuming Wcf Service into Angular JS Application. 我正在将Wcf Service消费到Angular JS应用程序中。 I am creating user login system by providing username and password. 我通过提供用户名和密码来创建用户登录系统。 But What ever username or password I enter in input filed its always display message username and password is correct . 但是,无论我在输入字段中输入的用户名或密码是什么,其始终显示的消息用户名和密码都是正确的。 I want if username and password is correct then I want to display message in angular js application otherwise username and password is not correct but I do not know why it is always hitting on if statement and does not matter if i enter wrong username or password. 我想如果用户名和密码正确,那么我想在angular js应用程序中显示消息,否则用户名和密码不正确,但是我不知道为什么它总是在if语句上起作用,并且如果我输入错误的用户名或密码也没关系。

Here is my Script code . 这是我的脚本代码。

  ///// <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));
    }
})

Here is my HTML Code .. 这是我的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>

Here is the out put .. 这是输出..

点击这里查看结果

It would help if you posted the response to your http POST request. 如果您将响应发布到您的http POST请求,这将有所帮助。 My bet is that even with incorrect login information you still make a valid http request and therefore get a valid http response with data (pl.data) that might indicate that the login info is incorrect. 我敢打赌,即使使用错误的登录信息,您仍然会发出有效的http请求,并因此获得带有数据(pl.data)的有效http响应,该数据可能表示登录信息不正确。

You will only hit the else statement if pl.data is null or undefined. 如果pl.data为null或未定义,则只会命中else语句。 Research javascript conditionals and how they work with objects. 研究javascript条件以及它们如何与对象一起使用。 If the object exists (not null or undefined) it is treated as true. 如果对象存在(不是null或未定义),则将其视为true。

In the then() block, first try to check the value of pl.data using the below code console.log(pl.data). 在then()块中,首先尝试使用下面的代码console.log(pl.data)检查pl.data的值。 With this, you will get to know what value is returning from the WCF service. 这样,您将了解WCF服务返回的值。 In case the service is returning True always, then in your Angular, it will always execute the if() statement. 如果服务始终返回True,那么在Angular中,它将始终执行if()语句。 Ex. 例如

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