我正在使用角度js mvc ie。用户界面,控制器和服务。我编写了一个代码,用于检查数据库中是否存在同名组织。 为此,我写了一个AJAX调用。如果名称存在于数据库中,则返回我,或者不是以' 0'或者' 1'。 即' 1'目前和' 0'缺席。我在控制器中检查这个响应,然后如果响应是一个,我将返回false给调用函数,但即使一切正常,我仍然可以看到变量值为' undefined'。 以下是代码:
控制器:
$scope.checkOrgName = function(){
console.log("We are checking id organization name "+$scope.organization.orgName+" is already present in the database or not");
if($scope.organization.orgName != ""){
OrganizationService.checkOrgName($scope.organization.orgName).then(
function(data){
var dbOrgName = data;
if(dbOrgName==1){
(Command comes here and shows the following alert box on the UI)
$scope.failureAlertMessage("This organization name is already present, enter some other name");
$scope.organization.orgName = "";
return false;(here I am returning false)
}
else if(dbOrgName==0){
console.log("Organization name is available");
return true;
}
},
function(errResponse){
$scope.failureAlertMessage("Error while checking organization name in the database");
}
);
}else{
console.log("No need for the organization name checking, name empty");
}
}
调用函数(仅限控制器内)
(Here I am calling the above function)
$scope.orgNameStatus = $scope.checkOrgName();
console.log("$scope.orgNameStatus: "+$scope.orgNameStatus);(This line prints 'undefined')
if($scope.orgNameStatus == false){
return false;
}
浏览器控制台:
Inside addOrganization
organization_controller.js:51 We are checking id organization name dsd is already present in the database or not
organization_service.js:25 In service: check org name
organization_controller.js:171 $scope.orgNameStatus: undefined
organization_controller.js:217 Validation status: true
organization_controller.js:59 Command in dbOrgName==1
代码有什么问题。请帮帮我,谢谢。!!
答案 0 :(得分:1)
$scope.orgNameStatus
未定义的原因是您的函数$scope.checkOrgName
未返回任何值。在函数的then
回调中只有一个return语句。
在任何情况下,由于该函数是异步的,它只会返回一个 Promise ,您可以再次注册一个回调。在继续处理之前,您需要确保请求已完成。
<强>控制器强>
$scope.checkOrgName = function(){
console.log("We are checking id organization name "+$scope.organization.orgName+" is already present in the database or not");
if($scope.organization.orgName != ""){
// added return statement to return promise
return OrganizationService.checkOrgName($scope.organization.orgName).then(
function(data){
var dbOrgName = data;
if(dbOrgName==1){
(Command comes here and shows the following alert box on the UI)
$scope.failureAlertMessage("This organization name is already present, enter some other name");
$scope.organization.orgName = "";
return false;(here I am returning false)
}
else if(dbOrgName==0){
console.log("Organization name is available");
return true;
}
},
function(errResponse){
$scope.failureAlertMessage("Error while checking organization name in the database");
}
);
}else{
console.log("No need for the organization name checking, name empty");
}
}
调用功能
$scope.checkOrgName().then(function(status) {
$scope.orgNameStatus = status; // now the request is complete and we can assign the status to the scope variable
console.log("$scope.orgNameStatus: "+$scope.orgNameStatus);(This line prints 'undefined')
if($scope.orgNameStatus == false){
return false;
}
});