gpt4 book ai didi

javascript - Angular JS 应用程序总是命中 If 语句,而不会继续执行 Else 语句

转载 作者:行者123 更新时间:2023-12-01 02:55:17 24 4
gpt4 key购买 nike

我正在将 Wcf 服务消费到 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>

这是输出..

click here to see the result

最佳答案

在 then() block 中,首先尝试使用以下代码检查 pl.data 的值控制台.日志(pl.数据)。这样,您将了解 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{
...
}

关于javascript - Angular JS 应用程序总是命中 If 语句,而不会继续执行 Else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46739778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com