I'm working with a parent and child component. The child component has the input field and will emit the value entered by the user to the parent component like this:
<parent-component (sendInputValue)="getInputValue($event)"><parent-component>
Now in parent component I have this:
getInputField(data){
console.log(data); // this prints the data (Example: abc)
// then here I'm just executing the API call ONLY if data length is 3
if(data.length === 3){
this.myService.getDataFromService(data).subscribe(rs=>console.log(rs));
}
}
Now let's say this happens:
So how to always execute API call if input data is 3 characters and if the user enters more characters nothing should happen, and if he deletes characters and goes back to the first 3 characters nothing should happen because the API already happened? Thanks a lot in advance!
I think you need distinctUntilChanged And you can use the filter in the pipe
this.myService.getDataFromService(data)
.pipe(
filter(_ => data.length === 3),
distinctUntilChanged()
).subscribe(rs => console.log(rs));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With