2017-04-25 24 views
0

我在電子郵件的表單中進行了驗證。如果用戶已經註冊,它將顯示警報,並用空值重置表單。我需要警報消息以及數據應保留在表單中。 在此先感謝。 https://plnkr.co/edit/WhdRmJmLs69yaf6Q2uYI?p=preview如何在angularjs中驗證錯誤後保留表單中的值

<div class="form-group"> 
 
       <label class="control-label col-sm-2" for="firstName">First Name:</label> 
 
       <div class="col-xs-4"> 
 
        <input type="text" class="form-control" id="firstName" ng-disabled="!edit" name="firstName" ng-model="user.firstName" ng-required="true"> 
 
        <span style="color:red" ng-show="userForm.myName.$touched && userForm.myName.$invalid">Please enter a First Name</span> 
 
       </div> 
 

 
       <label class="control-label col-sm-2">Email:</label> 
 
       <div class="col-xs-4"><input type="email" id="email" class="form-control" ng-disabled="!edit" name="email" ng-model="user.email" ng-required="true"> 
 
        <span style="color:red" ng-show="userForm.email.$touched && userForm.email.$invalid">Please enter valid email ID</span> 
 
       </div> 
 
      </div> 
 
<div class="panel-footer"> 
 
      <button type="button" ng-click='edit=!edit'id="Edit" ng-show="!edit" class="btn btn-default" >Edit</button> 
 
      <button ng-show="edit" id="save" type="submit" ng-click='addOrModifyUser(user)' class="btn btn-default" 
 
        ng-disabled="userForm.$invalid">Save 
 
      </button> 
 
      <a ng-if="user.accountId" ui-sref="account.locations.contacts({accountId: user.accountId})"> 
 
       <button type="type" class="btn btn-default">Cancel</button> 
 
      </a> 
 
      </div>

我?P =預覽

+0

將其保存在$ http調用之前的另一個變量中以驗證唯一性。 –

+0

你的plunk不是演示你有問題的工作演示。該HTML頁面甚至不包含Angular JS文件。 – Hoa

+0

您是否正在檢查電子郵件的唯一性?然後以指導方式行事。現在你正在使用'$ state.reload()'和'$ location.path()'來重新加載整個頁面,所以顯然你會丟失所有的數據。 –

回答

0

不知道你到底是什麼問題在這裏,但我這是怎麼會解決了角1定製的電子郵件驗證檢查。X。

HTML:

<form ng-submit="submit()"> 
    <input type="email" class="form-control" id="email" name="email" placeholder="email" ng-model="formModel.email" /> 
    <span ng-show="formModel.$submitted && formModel.teamName.$error.exists">Email already exists</span> 
</form> 

JS:(在你的角度控制器),其中 「EmailService」 將是首選,它檢查電子郵件存在對後端的您的HTTP服務:

myApp.controller('mainController', function($scope, EmailService) { 


    $scope.submit = function() { 

     $scope.formModel.email.$setValidity("exists", true); 
     EmailService.emailExist($scope.formModel.email, { // check if email exists 
      success: function(exists) { 

       $scope.$apply(function(){ 
        $scope.formModel.email.$setValidity("exists", !exists); // set validation flag 
       }); 

       if($scope.formModel.$valid) { 
        // submit if valid 
       } 
      } 
     } 
    } 
} 

祝你好運!

相關問題