2015-02-17 2 views
0

У меня есть форма angularjs, которая должна быть отправлена ​​на основании проверки подлинности электронной почты, но моя форма будет отправлена ​​до подтверждения обратной связи по электронной почте. вот контроллерФорма 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"); 
      }); 
     } 
    }; 

     }); 

}; 

регистра А ниже код для почтового Validation Service расслоения плотного файла

***************************** 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; 
     } 


    }; 
}); 

Пожалуйста, помогите мне решить этот выпуск

ответ

0

вы можете написать так.

emailId.then(function(data){ 

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

Причина: потому что службы звонки асинхронные, вы не можете гарантировать, что код будет выполняться последовательно, как написано.

+0

Hi @Anand Я наблюдаю еще одну вещь в моей консоли, что в emailId.then возвращает мне обещание, но оно не содержит данных, которые я ожидаю. Любые предложения – Hurix

+0

все еще сталкиваются с одной и той же проблемой :( – Hurix

Смежные вопросы