Cancel form in Angular

I need a form to be both $dirty, and not $submittedfor my verification.

I am trying to accomplish this through CSS using classes .ng-invalid, .ng-dirtyand .ng-submittedwhich are added dynamically by angular. This means that I cannot just set $submittedto false in the form, as this will not remove the associated class. I also cannot use $setPristineor $setUntouched, since they remove the flag $dirtyand its class.

Is there a way to set the form as unsubmitted so that the class is .ng-submitteddeleted and the form remains dirty?

+3
source share
2 answers

$setPristine, $setDirty. $dirty, , .

var formWasDirty = form.$dirty;

form.$setPristine(); 

if (formWasDirty) { 
  form.$setDirty();
}

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

+4

, $setUnsubmitted Angular.

$setSubmitted . , .

angular.module('YourModule').directive('form', function() {
    return {
        require: 'form',
        link: function(scope, element, attrs, form) {
            form.$setUnsubmitted = function() {
                element.removeClass('ng-submitted');
                form.$submitted = false;
                if (form.$$parentForm.$setUnsubmitted) { // may be nullFormCtrl
                    form.$$parentForm.$setUnsubmitted();
                }
            };
        }
    };
});

Angular , .

plunk

+4

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


All Articles