复位后的形式的验证消息不除去(Validation messages not removed aft

2019-10-29 06:28发布

我有一个HTML表单,我使用的角度JS进行验证。 在这里,提交表单后,我打电话复位方法将表单输入字段重置为默认值。

但是,当我提出和调用reset方法,验证消息出现在输入字段中。 我用下面的代码。 我不想在提交表单后看到的验证消息。

HTML

<form name="createvalidation" role="form" class="col-lg-12" novalidate>
    <div class="form-group">

    </div>
    <div class="form-group">
        <input type="text" placeholder="Challenge name" class="form-control input-sm" name="name" ng-model="challenge.challengename" required>
        <span ng-show="(createvalidation.name.$dirty || submitted) && createvalidation.name.$error.required">Name is reqiured</span>
    </div>

    <div class="form-group">
        <textarea class="form-control input-sm" rows="2" placeholder="Write more about this challenge..." name="description" ng-model="challenge.challengedescription" required></textarea>
        <span ng-show="(createvalidation.description.$dirty || submitted) && challengecreatevalidation.description.$error.required">Description is reqiured</span>
    </div>
</form>
<div>

    <button type="button" ng-click="createChallenge()">Create</button>
</div>

JS代码

 $scope.createChallenge = function() {
     //get the field and store in db
     $scope.resetForm();
 }
 $scope.master = {};
 $scope.resetForm = function() {

     $scope.challenge = angular.copy($scope.master);
     $scope.createvalidation.$setPristine();
 }

Answer 1:

$scope.createChallenge = function() {
  //get the field and store in db
 $scope.resetForm();
 }

 $scope.master = {};
 $scope.resetForm = function() {
 $scope.submitted = false; //Try adding this
 $scope.challenge = angular.copy($scope.master);
 $scope.createvalidation.$setPristine();

}



Answer 2:

你似乎从resetForm函数复制形式的原始状态。 这意味着,它只会有表格的版本时resetForm被调用。 相反,也许你应该做的是拷贝每当形式是建立和质朴。



文章来源: Validation messages not removed after resetting the form