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

前端 未结 10 815
梦毁少年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:27

    further to @battmanz 's answer, but without using any ES6 syntax to support older browsers.

     $scope.resetForm = function (form) {
    
                try {
                    var controlNames = Object.keys(form).filter(function (key) { return key.indexOf('$') !== 0 });
    
                    console.log(controlNames);
                    for (var x = 0; x < controlNames.length; x++) {
                        form[controlNames[x]].$setViewValue(undefined);
                    }
    
                    form.$setPristine();
                    form.$setUntouched();
                } catch (e) {                
                    console.log('Error in Reset');
                    console.log(e);
                }
    
            };
    

提交回复
热议问题