Angularjs表单在电子邮件验证之前提交

时间:2015-02-17 10:52:52

标签: angularjs angularjs-service angularjs-factory

我有一个angularjs表单,根据电子邮件验证提交,但我的表单在电子邮件验证回拨之前已经提交。这是寄存器控制器

**************** registerctrl.js **********************
   angular.module('myapp').controller('registerctrl',function($scope, emailValidationService,registerationValidationService ){
$scope.validateForm = function validateForm() {

    $scope.registerationForm.$submitted = true;
    $scope.registerationForm.email.$error.alreadyRegisteredEmailID = false;
    /*
        If the form is invalid it returns
    */
    var emailId = emailValidationService.getValidationResult($scope.user.email).then(function(promise){
        $scope.registerationForm.email.$error.alreadyRegisteredEmailID = promise.data;
        return promise.data;
    },function(promise){
        console.log("There is an error while validating the email ID"+$scope.user.email);
    });
    if (! registerationValidationService.validForm($scope.registerationForm)) {
        return;
    }
    /*
        If the email is already registerated returning the form
    */
    else if($scope.registerationForm.email.$error.alreadyRegisteredEmailID) {
        $scope.registerationForm.$invalid= true;
        $scope.registerationForm.email.$error.alreadyRegisteredEmailID = true;
        return;
    }
    /*
        If the form is valid saving the form and redirecting to success form
    */
    else{
        registerationValidationService.saveRegisterationForm($scope.user).then(function(promise){
            if(promise){
                $location.path('/registration-response');
            }
            else
                return;
        },function(promise){
            console.log("There is an error while submitting the form");
            });
        }
    };

      });

 };

以下代码用于电子邮件验证服务js文件

***************************** emailValidationService.js *****************
angular.module('myapp').factory('emailValidationService',function($http,$log){
return {
    getValidationResult : function(email){
        var promise = $http.get('data/email-exists.json').success(function(data,status,headers,config){
                //$http.post('/myapp/api/email-exists.json', { email: email }).success(function(data,status,headers,config){
                    console.log("Hey I am gng to return the value be ready to capture it ")
                    return data;
                }).error(function(data,status,headers,config){
                    console.log("Hey there is something went wrong here")
                    alert("Something went wrong");
                });
            return promise;
        }

    };
});

这是我的注册表格验证服务

********************* registerationValidationService *****************
angular.module('myapp').factory('registerationValidationService', function($http, $log){
return{
saveRegisterationForm : function(user) {

        var promise = $http.post('data/register.json', {
                firstName : user.firstName,
                lastName : user.lastName,
                email : user.email,
                mobileNumber : user.mobileNumber
            }).success(function(data, status, headers, config) {
                if (data.message == 'success') {
                    console.log("Data saved successfully")
                    return true;
                } else {
                    console.log("There is an error while saving the data");
                    return false;
                }
            }).error(function(data, status, headers, config) {
                console.log("Exception happened during your form submission have a look at post url and data");
            });
            return promise;
        }


    };
});

请帮我解决此问题

1 个答案:

答案 0 :(得分:0)

你可以这样写。

emailId.then(function(data){

  if($scope.registerationForm.email.$error.alreadyRegisteredEmailID){
    //what ever logic
  }
  else{
  }
});

原因:因为服务调用是异步的,所以无法保证代码会在写入时以串行方式执行。