In my form submission process, I need to independently validate my email ID. Once the email ID is validated using the validationService
, I will proceed with form submission and call formSubmissionService
from my controller.
I aim to implement the logic in my formController
as follows:
validationService.getEmailValidationResult.then(function(data){
$scope.validEmailId = data.exists;
});
if($scope.validEmailId == true){
formSubmissionService.submitForm().then(function(data){
$scope.form.submitted = data;
});
}
if($scope.form.submitted){
$location.path('formSubmissionResponse');
}
After receiving feedback from previous posts, it was pointed out that the $scopevalidEmailId cannot be accessed outside of the callback function.
Therefore, the code should be adjusted as shown below:
validationService.getEmailValidationResult.then(function(data){
$scope.validEmailId = data.exists;
if($scope.validEmailId){
formSubmissionService.submitForm.then(function(data){
$scope.form.submitted = data;
if($scope.form.submitted){
$location.path('formSubmissionResponse');
}
)};
}
});
Is there a better way to achieve this logic or can the rewritten code be further optimized?