Set Form to Pristine without clearing data

前端 未结 7 1527
暖寄归人
暖寄归人 2021-01-31 07:35

I have a form that displays a list of elements. They all share a common save button which is disabled until the form becomes dirty. Then

7条回答
  •  遥遥无期
    2021-01-31 07:37

    When using Angular's reactive forms, you can use markAsPristine() on the FormGroup:

    export class MyPage
    
      // Your form group
      public formGroup: FormGroup;
    
      ngOnInit() {
        this.formGroup = this.formBuilder.group({
          // Add your form controls here
        });
      }
    
      onSubmitForm() {
        // Get form value and save data on the server
        // ...
    
        this.formGroup.markAsPristine(); // <=== Mark form group as pristine
      }
    }
    

    See also: documentation for AbstractControl.markAsPristine(). Note: FormGroup is a subclass of AbstractControl. It will mark all children of the FormGroup as pristine.

提交回复
热议问题