I am trying to implement autoSearch in angular (+jqeuryEasyUI). I use mongoDB as the database.
In my controller, I wrote something like that:
$scope.$watch('searchInput.text', function() {
$timeout(functoin() {
$scope.performSearch($scope.searchInput.text);
}, 1500)
}
The function is called every time the user types another char in the search box input,
and the function: performSearch(searchInput) is send a GET request to the mongoDB to retrieve data for the current input string.
My problem is that if the user type,say, 3 letters , I want to cancel the request for the first letter and the request for the 2 first letter, and search only the 3 letter together.
That way I used the Timeout function, that any change the accuer in this time will cancel the previous request.
How can I mange the request? How do I tell the $watch function that if there was any change, don't send the request?
I heard of working with promises but I can't figure out how can it help in this situation.
Thank you very much!