angularjs form reset error

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

    I kept the type="reset" in my button. What I did was the ng-click="resetForm(userForm)" (using userFrom to match your example) and the controller defines resetForm() as

    scope.resetForm = function(controller) {
      controller.$commitViewValue();
      controller.$setPristine();
    };
    

    Here is what happens:

    1. When the reset button is clicked, it will bring back the original values as specified by the value attribute on the input
    2. The $commitViewValue() will force the write of whatever is on the view presently to the $modelValue of each field (no need to iterate manually), without this the last $modelValue would still be stored rather than reset.
    3. The $setPristine() will reset any other validation and submitted fields.

    In my angular-bootstrap-validator I already had the FormController as such I didn't need to pass in the form itself.

提交回复
热议问题