angularjs form reset error

前端 未结 13 1642
长发绾君心
长发绾君心 2020-12-03 04:14

i\'m trying to do a form with validations using angularjs and so far i did a good job. But when i commit my reset button all the fields reset except for the error messages i

13条回答
  •  攒了一身酷
    2020-12-03 05:01

    There's API documentation on the FormController.

    This allowed me to find that there's other methods to call such as:

    $setUntouched() - which is a function I was using if the user has focused on the field, and then left the field, this clears this feature when you run it.

    I created a simple form reset function which you can use too.

    // 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();
    }
    

    In your form, this simple setup will allow you to reset the form

提交回复
热议问题