I have a problem with my code and distinctUntilChanged operator.
This is my code:
ngAfterViewInit() {
fromEvent(this.headlineInput.nativeElement, 'blur').pipe(
takeUntil(this.unsubscribe$),
map((evt: any) => evt.target.value),
distinctUntilChanged()
).subscribe((text: string) => {
this.onInputValueChanges(text);
});
}
In this code, I would like to run this.onInputValueChanges(text) method on blur, but only once something has changed in headline input. If not, it should not run this method in subscribe.
I thought I can use distinctUntilChanged, but it seems to not working. It runs anytime I make a blur from my headline input. Am I doing something wrong? Can someone point me out what is wrong in this code? Thanks!
distinctUntilChanged((prev, curr) => prev.something === curr.something).@Input() inputValue: string;My method works, but only once I ran it second time, it's not working on first time, because myinputValueis undefined onngAfterViewInit. On my second blur it's working properly. At your stackblitz, you addedvalue="John Doe"I think that's why it works in your case because it has something to compare