Angular Compomente Personalized emitEvent: false

Asked

Viewed 45 times

0

I need my custom component when making a value assignment it does not send events, when I set it in patchValue or setValue.

patchValue { onlySelf: true, emitEvent: false }

Stackblitz Demo

when setting the initial value it fires the this.onChangeCallback(v)

set value(v: any) {
   this.onChangeCallback(v);
}

onChangeCallback: (_: any) => void = () => {};

ignoring the emitEvent: false

2 answers

1

After the loading of your form is issued a single event for the lastName who writes a message on the console:

event.last.name Reis 

This is because your second input is a custom component (app-input-text) that implements the ControlValueAccessor. The problem is that the function that is passed as parameter by the framework in the method writeValue does not receive the second method parameter this.form.patchValue. There is even an open question about this behavior at this link.

You could try a workaround using a boolean variable for example, or change your code so you don’t need this feature.

0

Solved the problem was in writeValue

traded

writeValue(v: any): void {
  this.value = v;
}

for

writeValue(v: any): void {
 if (v !== this.innerValue) {
   this.innerValue = v;
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.