tamtakoe
tamtakoe

Reputation: 237

How to prevent double calling of onChange callback in reactive form of Angular2+?

Demo: https://stackblitz.com/edit/angular-svpg8g

I have some third part component (ThirdPartCounter in the app/counter.component.ts). I've wrapped it into the Angular component and subscribed on changing of this counter. When I interact with counter UI (Increment button) I have correct events in my stream. But! If I setup counter value manually (Clear button) I have duplicate of event in this stream (one is because component changing, next called after writeValue).

How should I prevent excess event after writeValue callback?

Upvotes: 1

Views: 616

Answers (1)

Fateh Mohamed
Fateh Mohamed

Reputation: 21387

try this , it will not duplicate with emitEvent to false

 clear() {
  this.myForm.controls['counter'].patchValue(0, {emitEvent : false});
 }

check it here https://stackblitz.com/edit/angular-dc8cyl

Upvotes: 1

Related Questions