How do I reset a form including removing all validation errors?

前端 未结 10 928
梦毁少年i
梦毁少年i 2020-12-14 01:59

I have an Angular form. The fields are validated using the ng-pattern attribute. I also have a reset button. I\'m using the Ui.Utils Event Binder to handle the

10条回答
  •  [愿得一人]
    2020-12-14 02:33

    So none of the answers were completely working for me. Esp, clearing the view value, so I combined all the answers clearing view value, clearing errors and clearing the selection with j query(provided the fields are input and name same as model name)

     var modelNames = Object.keys($scope.form).filter(key => key.indexOf('$') !== 0);
                        modelNames.forEach(function(name){
                            var model = $scope.form[name];
                            model.$setViewValue(undefined);
                            jq('input[name='+name+']').val('');
                            angular.forEach(model.$error, function(value, name) {
                              // reset validity
                              model.$setValidity(name, null);
                            });
                        });
                        $scope.form.$setPristine();
                        $scope.form.$setUntouched();
    

提交回复
热议问题