Angular $setPristine() not working

后端 未结 1 1504
遥遥无期
遥遥无期 2021-01-18 17:32

I\'m trying to use Angular\'s built-in form functions, specifically setPristine() to clear the form on user submit. My controller has access to $scope.new

相关标签:
1条回答
  • 2021-01-18 18:11

    $setPristine only marks the form as being $pristine, which is useful for validation-driven expressions and CSS (e.g. .ng-dirty)

    So, $setPristine does not clear the form's controls. In fact, it wouldn't even know how to do that. Consider, that to "clear" could mean different things to different models. "Clear" could mean "", or undefined, or null, or anything at all that a custom input control that works with ngModel could mean.

    So, to properly clear the form is to modify the View Model that drives the form to whatever definition of "clear" it needs. In most cases - yours included - it is just a matter of setting the View Model to a new object:

    $scope.submit = function(place) {
       $scope.newForm.$setPristine();
       $scope.newForm.$setUntouched();
    
       // clear the form
       $scope.place = {};
    };
    
    0 讨论(0)
提交回复
热议问题