$ setPristine () does not work properly if the form control has invalid data

I am trying to create a reset form using $ setPristine ().

$scope.resetDataEntryForm = function() {
    $scope.dataEntryForm.$setPristine();
    $scope.pr = {};
};

It works great if all input controls are in the correct state. one type of input is a URL. For example, if I have an invalid URL value when I press reset. if the reset code above contains the contents of the URL input field and mark the error false. I need this for verification.

To make a correct reset, I need to manually reset all error flags

$scope.resetDataEntryForm = function() {
    $('#dataEntryForm')[0].reset();
    $scope.dataEntryForm.$setPristine();
    $scope.dataEntryForm.name.$error.required = true;
    $scope.dataEntryForm.site.$error.required = true;
    $scope.dataEntryForm.site.$error.url = false;
    $scope.pr = {};
};

Can anyone suggest the correct way to reset form using angular.js?

+3
source share
1 answer

, , $setPristine() $error.

setPristine

    form.$setPristine = function () {
        element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);
        form.$dirty = false;
        form.$pristine = true;
        forEach(controls, function(control) {
          control.$setPristine();
        });
      };

( ..)

    this.$setPristine = function () {
      this.$dirty = false;
      this.$pristine = true;
      $element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS);
    };  

, < reset $error

PS: AngularJs 1.2. *, Github https://github.com/angular/angular.js/blob/291d7c467fba51a9cb89cbeee62202d51fe64b09/src/ng/directive/form.js

0

All Articles