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

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

    Following worked for me

    let form = this.$scope.myForm;   
    let controlNames = Object.keys(form).filter(key => key.indexOf('$') !== 0);
    for (let name of controlNames) {
        let control = form [name];
        control.$error = {};
    }
    

    In Short: to get rid of ng-messages errors you need to clear out the $error object for each form item.

提交回复
热议问题