angularjs形成重置错误

Cra*_*oiD 39 html javascript validation angularjs

我正在尝试使用angularjs进行验证表格,到目前为止我做得很好.但是当我提交我的重置按钮时,所有字段都会重置,除了我从验证部分获得的错误消息.当我重置表单时,如何摆脱所有字段和错误消息.

按下我的重置按钮时就是这样

在此输入图像描述

这是我的代码

<div class="page-header"><center><h2>Give us your Feedback</h2></center></div>

    <!-- pass in the variable if our form is valid or invalid -->
    <form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>

        <!-- NAME -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$dirty }">
            <label>Name*</label>
            <input type="text" name="name" class="item-input-wrapper form-control" ng-model="user.name"  required>
            <p ng-show="userForm.name.$invalid && !userForm.name.$pristine " class="help-block">
                <font color="#009ACD">You name is required.</font>
            </p>
        </div>

         <!-- EMAIL -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$dirty  }">
            <label>Email</label>
            <input type="email" name="email" class="item-input-wrapper form-control" ng-model="user.email" required >
            <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">
                <font color="#009ACD">Enter a valid email.</font>
            </p>
        </div>

        <!-- USERNAME -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.username.$invalid && !userForm.username.$dirty }">
            <label>Description</label>
            <input type="text" name="username" class="item-input-wrapper form-control" ng-model="user.username"  ng-minlength="5" ng-maxlength="60" required>
            <font color="white">
                <p ng-show="userForm.username.$error.minlength" class="help-block">
                    <font color="#009ACD">Description is too short.</font>
                </p>
                <p ng-show="userForm.username.$error.maxlength" class="help-block">
                    <font color="#009ACD">Description is too long.</font>
                </p>
            </font>
        </div>

        <div class="col"style="text-align: center">
            <button align="left"class="button button-block button-reset"style="display: inline-block;width:100px;text-align:center "
                type="reset"
                ng-click="reset()" padding-top="true"
            >
                Reset
            </button>


            <button class="button button-block button-positive"  style="display: inline-block;width:100px "
                ng-click="submit()"
                padding-top="true"
            >
                Submit
            </button>
        </div>

    </form>
</div>
Run Code Online (Sandbox Code Playgroud)

我的控制器

.controller('ContactCtrl', function($scope,$state,$ionicPopup, $timeout) {
    $scope.showfeedback = function() {
        $state.go('app.sfeedback');
    };

    $scope.submitForm = function(isValid) {
        $scope.submitted = true;

        // check to make sure the form is completely valid
        if (!isValid) {
            var alertPopup = $ionicPopup.alert({
                title: 'Invalid data entered!',
            });
        } else {
            var alertPopup = $ionicPopup.alert({
                title: 'Feedback submitted',
            });
        }
    };

    $scope.reset = function() {
        var original = $scope.user;
        $scope.user= angular.copy(original)
        $scope.userForm.$setPristine()
    };
})
Run Code Online (Sandbox Code Playgroud)

K.T*_*ess 41

var original = $scope.user;
Run Code Online (Sandbox Code Playgroud)

重置时:

$scope.user= angular.copy(original);
$scope.userForm.$setPristine();
Run Code Online (Sandbox Code Playgroud)

去掉

type='reset' in  <button>
Run Code Online (Sandbox Code Playgroud)

这是表单控制器的Angular 文档.

  • 不要忘记尝试在angular 1.3上引入$ scope.userForm.$ setUntouched()/ $ touch flag! (11认同)

Tha*_*age 32

使用以下命令重置脏状态

$scope.form.$setPristine();
Run Code Online (Sandbox Code Playgroud)

使用以下命令重置以清除验证

$scope.form.$setValidity();
Run Code Online (Sandbox Code Playgroud)


Sha*_*ins 24

FormController上有API文档.

这让我发现还有其他方法可以调用,例如:

$setUntouched() - 如果用户专注于该字段,然后离开该字段,这是我正在使用的功能,这将在您运行它时清除此功能.

我创建了一个简单的表单重置功能,您也可以使用它.

// Set the following in your controller for the form/page.
// Allows you to set default form values on fields.
$scope.defaultFormData = { username : 'Bob'}
// Save a copy of the defaultFormData
$scope.resetCopy = angular.copy($scope.defaultFormData);
// Create a method to reset the form back to it's original state.
$scope.resetForm =  function() {
    // Set the field values back to the original default values
    $scope.defaultFormData = angular.copy($scope.resetCopy);
    $scope.myForm.$setPristine();
    $scope.myForm.$setValidity();
    $scope.myForm.$setUntouched();
    // in my case I had to call $apply to refresh the page, you may also need this.
    $scope.$apply();
}
Run Code Online (Sandbox Code Playgroud)

在您的表单中,这个简单的设置将允许您重置表单

<form ng-submit="doSomethingOnSubmit()" name="myForm">
    <input type="text" name="username" ng-model="username" ng-required />
    <input type="password" name="password" ng-model="password" ng-required />
    <button type="button" ng-click="resetForm()">Reset</button>
    <button type="submit">Log In</button>
</form>
Run Code Online (Sandbox Code Playgroud)