如何控制AngularJS中的函数执行流程?

时间:2016-08-18 14:30:06

标签: angularjs

我在AngularJS控制器中编写了一些代码。 现场如下,我有一个表格接受组织的所有细节,包括其名称,管理员等。我已经编写了手动验证来验证数据。我编写了一个单独的函数来验证数据。这将验证所有数据,并根据验证函数返回" true / false" ,我想检查返回的值,如果值' true& #39; ,我想调用服务将数据输入数据库,,如果结果为 false 我不应该调用该服务并显示正确的警报消息。 但问题是,我调用验证函数的地方,它不等待函数返回值,而是先执行它然后将值返回给调用函数,如结果它始终显示状态为"未定义"

验证功能如下:

 $scope.validateOrganizationForm = function(){

          var phoneNumber = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;  

         if($scope.organization.orgName == "" || $scope.organization.orgName.length > 16){
             $scope.failureAlertMessage("Please enter valid organization name of length between 1 to 16");
             return false;
         }else if($scope.organization.orgAddress == "" || $scope.organization.orgAddress.length > 512){
             $scope.failureAlertMessage("Please enter valid organization address of length between 1 to 512");
             return false;
         }else if($scope.organization.orgPhoneNumber == "" || $scope.organization.orgPhoneNumber.length>13 || (!($scope.organization.orgPhoneNumber.match(phoneNumber)))){
             $scope.failureAlertMessage("Please enter valid organization phone number of length between 10 to 13");
             return false;
         }else if($scope.organization.orgAlternatePhoneNumber != "" && ($scope.organization.orgAlternatePhoneNumber.length>13 ||  (!($scope.organization.orgAlternatePhoneNumber.match(phoneNumber)))) ){
             $scope.failureAlertMessage("Please enter valid organization alternate phone number of length between 10 to 13");
             return false;
         }else if($scope.organization.orgContactPersonName == "" || $scope.organization.orgContactPersonName.length > 64){
             $scope.failureAlertMessage("Please enter valid organization contact person name of length between 1 to 64");
             return false;
         }else if($scope.organization.orgContactPersonPhoneNumber == "" ||  (!($scope.organization.orgContactPersonPhoneNumber.match(phoneNumber)))){
             $scope.failureAlertMessage("Please enter valid organization contact person phone number of length between 10 to 13");
             return false;
         }else if($scope.organization.orgContactPersonEmailId == "" || $scope.organization.orgContactPersonEmailId == undefined || $scope.organization.orgContactPersonEmailId.length > 64){
             $scope.failureAlertMessage("Please enter valid organization contact person email id of length between 1 to 64");
             return false;
         }else if($scope.organization.orgDescription == "" || $scope.organization.orgDescription.length > 256){
             $scope.failureAlertMessage("Please enter valid organization description of length between 1 to 256");
             return false;
         }else if($scope.organization.userVo.username == "" || $scope.organization.userVo.username.length > 64){
             $scope.failureAlertMessage("Please enter valid organization admin username of length between 1 to 64");
             return false;
         }else if($scope.organization.userVo.emailId == "" || $scope.organization.userVo.emailId.length > 128){
             $scope.failureAlertMessage("Please enter valid organization admin email Id of length between 1 to 128");
             return false;
         }else{
             console.log("All the fields are validated except validating duplications, lets check it now");
             $scope.checkOrgName().then(
                     function(orgNameStatus){
                         console.log("can we use this org name :"+orgNameStatus);
                         if(orgNameStatus==false){
                             console.log("Organziation name is already present in the database");
                             return false;
                         }else if(orgNameStatus){
                             $scope.isOrgAdminUsernameAvailable = $scope.checkOrgAdminUsername().then(
                                     function(orgAdminUsernameStatus){
                                         console.log("Can we use this username for admin: "+orgAdminUsernameStatus);
                                         if(orgAdminUsernameStatus == false){
                                             console.log("org admin username is already present in the database");
                                             return false;
                                         }else if(orgAdminUsernameStatus){
                                             $scope.isAdminEmailIdAvailable = $scope.checkOrgAdminEmailId().then(
                                                     function(orgAdminemailIdStatus){
                                                         console.log("Can we use this admin email id: "+orgAdminemailIdStatus);
                                                         if(orgAdminemailIdStatus == false){
                                                             console.log("Org admin email id is already present in the database");
                                                             return false;
                                                         }else if(orgAdminemailIdStatus){
                                                             console.log("All the three fields(orgName,orgAdminUsername,orgAdminEmailId) are checked for duplication, everything is validated sucessfully");
                                                             return true;
                                                         }
                                                     });
                                         }
                                     });
                         }
                     });
         }
         return true;
     }

以下是调用函数:

$scope.addOrganization = function(){
         console.log("Command in add Org");
         var validationStatus = $scope.validateOrganizationForm(); (Here I am calling validation function)

                     console.log("Add form validation status: "+validationStatus); (This always gives me undefined...)
                     if(validationStatus){
                         console.log("Form validated successfully, We can add organization safely");
                         OrganizationService.addOrganization($scope.organization).then(
                            function(data){
                                $scope.successAlertMessage("Organition Added successfully");
                                $scope.resetAddOrgForm();
                            },
                            function(errResponse){
                                $scope.successAlertMessage("There was an error adding organization");
                            }
                         );
                     }else{
                         console.log("Add form validation failed");
                     }


      }

我可以使用 .then(),我也试过了,但它说我没有返回任何承诺而是我只是返回一个普通的布尔值,我怎么能发送承诺如果我必须使用 .then() .. ??

0 个答案:

没有答案