Something strange happening here. I have a form with 3 fields: a string
and two objects
.
The both objects get values from mat-select
. Here, if I select again the current value, will not be emitted any event. For the string, is another situation (named country
)
The country field is a value get from one of three buttons as:
ts:
countries = ['en', 'de', 'es'];
countryChanged(country: string): void {
this.form.patchValue({
country
});
}
html:
<button *ngFor="let country of countries" (click)="countryChanged(country)">
{{country}}
</button>
now, the problem is that, even if I press the same button, it will trigger again the form valueChanges
, even the value is the same as the previous.
this.form.valueChanges.pipe(
distinctUntilChanged() // ?????
).subscribe(data => {
// do stuff
});
Why distinctUntilChanged()
don't do its work here?
I know I can avoid it simple, with an if condition inside countryChanged()
method, but I want to understand why distinctUntilChanged()
don't do its job.
countryChanged(country: string): void {
if (country === this.form.get('country').value) {
return;
}
this.form.patchValue({
country
});
}