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

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

          $scope.search = {areaCode: xxxx, phoneNumber: yyyy}
    

    Structure all models in your form in one place like above, so you can clear it like this:

          $scope.search = angular.copy({});
    

    After that you can just call this for reset the validation:

          $scope.search_form.$setPristine();
          $scope.search_form.$setUntouched();
          $scope.search_form.$rollbackViewValue();
    

提交回复
热议问题