AngularJs .$setPristine to reset form

后端 未结 6 662
耶瑟儿~
耶瑟儿~ 2020-12-05 09:15

I been struggling to reset form once form is submitted. Someone posted this Here which I want to make it work but no success. Here is my My Code Example.

$scop

6条回答
  •  孤街浪徒
    2020-12-05 09:38

    Just for those who want to get $setPristine without having to upgrade to v1.1.x, here is the function I used to simulate the $setPristine function. I was reluctant to use the v1.1.5 because one of the AngularUI components I used is no compatible.

    var setPristine = function(form) {
        if (form.$setPristine) {//only supported from v1.1.x
            form.$setPristine();
        } else {
            /*
             *Underscore looping form properties, you can use for loop too like:
             *for(var i in form){ 
             *  var input = form[i]; ...
             */
            _.each(form, function (input) {
                if (input.$dirty) {
                    input.$dirty = false;
                }
            });
        }
    };
    

    Note that it ONLY makes $dirty fields clean and help changing the 'show error' condition like $scope.myForm.myField.$dirty && $scope.myForm.myField.$invalid.

    Other parts of the form object (like the css classes) still need to consider, but this solve my problem: hide error messages.

提交回复
热议问题