[Fixed] I am subscribing for data in the same function which gets called every time I type something in the textbox right approach in Angular?


I have an input box which triggers getNumber function every time when I type something in it and I am subscribing for data in that same function.
getNumber function calls function in service which makes a back-end API call and returns data. Is subscribing again and again in the same function right approach?


right now you’re calling the API on every keystroke. If you really do want that to happen, then yes, you do actually subscribe each time.

subscribe() on httpclient calls (assuming that’s what this service uses) will immediately call and then be completed. it’s mostly like a ajax call/promise. so each time you want to actually send the request to the backend, you do actually need to call subscribe. Many observables do not work like this because they don’t complete immediately, but this one does simply because it is a fire once and complete.

Leave a Reply

(*) Required, Your email will not be published