How do I know when custom form control is marked as pristine in Angular?

后端 未结 3 2023
再見小時候
再見小時候 2020-12-10 04:03

I have several custom form control components in my Angular application, which implement ControlValueAccessor interface and it works great.

However, whe

3条回答
  •  既然无缘
    2020-12-10 04:29

    Another suggestion, based upon Slava's answer, is to replace the markAsDirty, markAsPristine, and _updatePristine methods in the FormGroup class:

    ngOnInit(): void {
      const markAsDirty = this.formGroup.markAsDirty;
      this.formGroup.markAsDirty = (opts) => {
        markAsDirty.apply(this.formGroup, opts);
        console.log('>>>>> markAsDirty');
      };
      const markAsPristine = this.formGroup.markAsPristine;
      this.formGroup.markAsPristine = (opts) => {
        markAsPristine.apply(this.formGroup, opts);
        console.log('>>>>> markAsPristine');
      };
      const updatePristine = this.formGroup['_updatePristine'];
      this.formGroup['_updatePristine'] = (opts) => {
        updatePristine.apply(this.formGroup, opts);
        console.log('>>>>> updatePristine');
      };
    }
    

    I'm emitting events in the console.log locations, but other approaches would work, of course.

提交回复
热议问题