AngularJs .$setPristine to reset form

后端 未结 6 641
耶瑟儿~
耶瑟儿~ 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.

    0 讨论(0)
  • 2020-12-05 09:42

    There is another way to pristine form that is by sending form into the controller. For example:-

    In view:-

    <form name="myForm" ng-submit="addUser(myForm)" novalidate>
        <input type="text" ng-mode="user.name"/>
         <span style="color:red" ng-show="myForm.name.$dirty && myForm.name.$invalid">
          <span ng-show="myForm.name.$error.required">Name is required.</span>
        </span>
    
        <button ng-disabled="myForm.$invalid">Add User</button>
    </form>
    

    In Controller:-

    $scope.addUser = function(myForm) {
           myForm.$setPristine();
    };
    
    0 讨论(0)
  • 2020-12-05 09:44

    Had a similar problem, where I had to set the form back to pristine, but also to untouched, since $invalid and $error were both used to show error messages. Only using setPristine() was not enough to clear the error messages.

    I solved it by using setPristine() and setUntouched(). (See Angular's documentation: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController)

    So, in my controller, I used:

    $scope.form.setPristine(); 
    $scope.form.setUntouched();
    

    These two functions reset the complete form to $pristine and back to $untouched, so that all error messages were cleared.

    0 讨论(0)
  • 2020-12-05 09:48

    $setPristine() was introduced in the 1.1.x branch of angularjs. You need to use that version rather than 1.0.7 in order for it to work.

    See http://plnkr.co/edit/815Bml?p=preview

    0 讨论(0)
  • 2020-12-05 10:00

    DavidLn's answer has worked well for me in the past. But it doesn't capture all of setPristine's functionality, which tripped me up this time. Here is a fuller shim:

    var form_set_pristine = function(form){
        // 2013-12-20 DF TODO: remove this function on Angular 1.1.x+ upgrade
        // function is included natively
    
        if(form.$setPristine){
            form.$setPristine();
        } else {
            form.$pristine = true;
            form.$dirty = false;
            angular.forEach(form, function (input, key) {
                if (input.$pristine)
                    input.$pristine = true;
                if (input.$dirty) {
                    input.$dirty = false;
                }
            });
        }
    };
    
    0 讨论(0)
  • 2020-12-05 10:03

    I solved the same problem of having to reset a form at its pristine state in Angular version 1.0.7 (no $setPristine method)

    In my use case, the form, after being filled and submitted must disappear until it is again necessary for filling another record. So I made the show/hide effect by using ng-switch instead of ng-show. As I suspected, with ng-switch, the form DOM sub-tree is completely removed and later recreated. So the pristine state is automatically restored.

    I like it because it is simple and clean but it may not be a fit for anybody's use case.

    it may also imply some performance issues for big forms (?) In my situation I did not face this problem yet.

    0 讨论(0)
提交回复
热议问题