Disable valueChange due to a writeValue

188 Views Asked by At

On my angular component, which implements ControlValueAccessor, I'm having a problem with fields getting blanked. I'm doing this:

ngOnInit() {
   this.formGroup.get('name')?.valueChanges.subscribe(x => {
       this.formGroup.patchValue(version: null)
   })
}

writeValue(...) {
   this.formGroup.setValue({
      name: ...,
      version: ...
   })

   this.changeDetectorRef.markForCheck()
}

If they update the name, they have to pick a new version. My problem is that when writeValue gets called, that triggers the valueChanges operations, and so even though I gave a version, the version is always empty.

How do I work around that?

1

There are 1 best solutions below

1
On

You can prevent valueChanges from being called by passing an additional object to your setValue call as so: { emitEvent: false }