patchValue with { emitEvent: false } triggers valueChanges on Angular 4 formgroup
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?
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)"/>