How can I check and use ... $ setPristine (); in the form of AngularJS?

I have the following code:

            <form class="form"
                  data-ng-submit="modalSubmit(modal.data)"
                  id="modal-body"
                  name="modalForm"
                  novalidate>

This works, and when I press a submit button, the modalSubmit function is called.

However, I would like to do this in my controller:

$scope.modalForm.$setPristine();  

But this leads to an error:

has no method '$setPristine'

How can I set a form intact? I tried to add data-ng-form="modalForm", but then a message talking about the effect of duplicate directive names.

I tried changing the form element to DIV, but then clicking on the submit button does not call the function

Here is an example (modified from another user) that shows what I'm trying to do, which is set for the values ​​intact:

plnkr.co/edit/LNanJdAggMLIgxii0cfv?p=preview

+4
source share
3

, , angular, $setPristine() . $setPristine() 1.1. +, angular, . , , 1.2. +.

, $ $ :

$scope.mp = function() {
  $scope.mainForm.$pristine=true;//clean main form
  $scope.mainForm.$dirty=false;
  angular.forEach($scope.mainForm,function(input){//clean all input controls
    if (input !== undefined && input.$dirty !== undefined) {
      input.$dirty=false;
      input.$pristine=true;
    }
  });
}
+6

-, angular , 1.2.12 - CDN. $setPristine - HTML5, .

, required ng-required. angular. novalidate .

http://plnkr.co/edit/l1mUCceSFMFFZWgGgL6u?p=preview

+3

it is already implemented in this link , which you can use, it was, as was shown in the plnkr link. As can be seen from the above description, $ setPristine only changes the state of the form (and thereby resets the css applied to each control on the form).

If you want to clear the values ​​of each control, you need to do for each in the code.

0
source

Source: https://habr.com/ru/post/1529368/


All Articles