0

In HTML I have this:

<input id="Search" type="text" placeholder="Search Images.." ng-model="data" 
       ng-keypress="($event.charCode==13)? searchMore() : return">

This is basically input field which acts as search.

In controller I just receive the value which is passed to input: $scope.data

I want to determine somehow if the search term has changed so that I can wipe out results array if it did or append if just more results of the same search term came.

How to determine that $scope.data now contains a different string? If I will just set something like this:

$scope.savedData = $scope.data 

This new variable will be overwritten all the time with new data.

1 Answer 1

2

ng-change is one easy way

<input id="Search" 
      type="text" 
      placeholder="Search Images.." 
      ng-model="data" 
      ng-change="clearResults()"
      ng-keypress="($event.charCode==13)? searchMore() : return">
Sign up to request clarification or add additional context in comments.

2 Comments

ng-change is triggered on every key stroke and I am doing an API call only on keystroke (enter). Doing API calls on every key stroke might not be the best approach.
@funguy You can use debounce for ng-model-options, so for example with ng-model-options: {debounce: 1000} when user finish typing it will wait a 1000 miliseconds and then update model and fetch data. Of course it should react as-is on enter. And it depends on you how you implement clearResults - it can be simple boolean flag, without call itself. For call you have searchMore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.