In my current project, I am dealing with a parent and child component setup. The child component includes an input field that will emit the user-entered value to the parent component using the following syntax:
<parent-component (sendInputValue)="getInputValue($event)"><parent-component>
Within the parent component, I have the following code snippet:
getInputField(data){
console.log(data); // This displays the data (Example: abc)
// Here, I trigger an API call ONLY if the length of data is 3
if(data.length === 3){
this.myService.getDataFromService(data).subscribe(rs=>console.log(rs));
}
}
Now, let's consider the following scenarios:
- The user enters "abc" // API call is executed successfully
- User enters "abcd" // No additional API call is triggered
- User deletes the letter "d", resulting in the new data value being "abc" - In this case, no extra API call should be made as it was already done for "abc"
- If the user removes the letter "c", making the data value "ab", there should not be any API call expected
- If the user adds back the letter "c", making the value "abc", then the API call is expected, as intended
To always execute the API call when the input data consists of 3 characters, prevent any action on adding more characters, and ensure no repeated calls for the same initial 3-character sequence once deleted, requires careful handling of the logic. Thank you in advance for your guidance!