I have a formbuilder group and am listening for changes with valueChanges and triggering a save function followed by refresh function on the form:
this.ticketForm.valueChanges.debounceTime(1000).distinctUntilChanged()
.subscribe(data => {
this.saveTicket();
this.refreshTicket();
})
I am then reloading the form and repatching the data to form fields (and elsewhere on the page, particularly a change log) with patchValue, e.g.:
this.ticketForm.patchValue(ticket, { emitEvent: false });
however, this causes an infinite loop of saves of the form despite emitEvent : false.
Is this an Angular 4/Ionic 3 bug or a misunderstanding on my part?
So basically when we call setValue, patchValue, disable, enable control it triggers value change by default. If we had not setted emitEvent to false, it would trigger valueChange so parent form would trigger unnecessary api which we want to avoid here.
SetValue Vs PatchValue The difference is that with setValue we must include all the controls, while with the patchValue you can exclude some controls.
FormGroup.patchValue The patchValue accepts the object with control names as keys. If the supplied object does not contain all the form controls as keys of this FormGroup object, patchValue sets new value only to those form controls which are available in supplied object .
onlySelf : When true, each change only affects this control, and not its parent. Default is false. emitEvent : When true or not supplied (the default), both the statusChanges and valueChanges observables emit events with the latest status and value when the control is reset. When false, no events are emitted.
Try adding onlySelf: true
along with the emitEvent: false
in this way:
this.ticketForm.patchValue(ticket, {emitEvent: false, onlySelf: true});
Can't comment because of rep, so I will post it as an answer to @Craig Wayne.
emitEvent:false works only if you are listening to value changes on the form control with:
this.form.valueChanges.controlName.subscribe(val => doSomething(val));
if you are binding to model changes on the element event is emitted regardless:
<input (ngModelChange)="doSomething($event)"/>
While working with Angular 9.1.13 I had been facing the same problem.
Tried to use the FormControl.setValue
, and FormGroup.patchValue
APIs, using also the suggested params {emitEvent: false, onlySelf: true}
in all possible combinations.
The valueChanges
observable is was still being triggered.
The only thing that worked for me eventually was :
myForm.disable();
myForm.patchValue({myControl: ''}, {onlySelf: true, emitEvent: false});
myForm.enable();
As a workaround, i add skip to rxjs pipe
this.form.valueChanges
.pipe(skip(1)).subscribe();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With