gpt4 book ai didi

javascript - 使用 AngularJS 注册

转载 作者:行者123 更新时间:2023-12-03 05:22:08 25 4
gpt4 key购买 nike

我想使用 AngularJS 进行一个非常简单的注册。首先,我通过此电子邮件获取用户并将其分配给 $scope.users。如果方法“GetUserByEmail”返回多个用户,我会尝试显示消息“用户已存在”。这是问题。避免使用 GetUserByEmail 方法。程序“跳转”到“if”条件并且 $scope.users 始终为空,我不知道为什么。有时将用户添加到数据库后,该方法会返回对象数组并分配给 $scope.users

这是我使用 CreateUser 方法的代码:

var RegisterController = function ($scope, Api, $http) {

$scope.users = {
}

$scope.CreateUser = function () {
var user = {
Password: $scope.password,
Name: $scope.name,
Surname: $scope.surname,
Email: $scope.email,
DateOfBirth: $scope.dateofBirth
}

Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).then(function (d) {
$scope.users = d;
});

if ($scope.users.length > 0)
{
alert("User already exists!");
$scope.users = {};
}
else
{
Api.PostUser("Users", "PostUser", user).then(function (d) {
alert("Hello");
});
}


};
}

RegisterController.$inject = ['$scope', 'Api', '$http'];

以及方法 GetUserByEmail:

this.GetUserByEmail = function (controllerName, methodName, email) {
var promise = $http({
method: 'GET',
url: 'api/' + controllerName + '/' + methodName + '?email=' + email,
config: {
params: {
"email": email
}
}
})
.then(function onSuccess(response) {
return response.data;
},
function onError(response) {
return response.statusText;
});
return promise;
}

最佳答案

试试这个!

var RegisterController = function($scope, Api, $http) {

$scope.users = {}

$scope.CreateUser = function() {
var user = {
Password: $scope.password,
Name: $scope.name,
Surname: $scope.surname,
Email: $scope.email,
DateOfBirth: $scope.dateofBirth
}

Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).then(function(d) {
$scope.users = d;
if ($scope.users.length > 0) {
alert("User already exists!");
$scope.users = {};
} else {
Api.PostUser("Users", "PostUser", user).then(function(d) {
alert("Hello");
});
}
});

};
}

RegisterController.$inject = ['$scope', 'Api', '$http'];

关于javascript - 使用 AngularJS 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41331003/

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