angularjs form reset error

前端 未结 13 1691
长发绾君心
长发绾君心 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

    I had the same problem and used the following code to completely reset the form :

    $scope.resetForm = function(){
    
        // reset your model data
        $scope.user = ...
    
        // reset all errors
        for (var att in  $scope.userForm.$error) {
            if ($scope.userForm.$error.hasOwnProperty(att)) {
                $scope.userForm.$setValidity(att, true);
            }
        }
    
        // reset validation's state
        $scope.userForm.$setPristine(true);
    };
    

提交回复
热议问题