@@ -78,7 +78,6 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
7878 var inputField = elem . find ( 'input' ) ;
7979 var minlength = MIN_LENGTH ;
8080 var searchTimer = null ;
81- var lastSearchTerm = null ;
8281 var hideTimer ;
8382 var requiredClassName = REQUIRED_CLASS ;
8483 var responseFormatter ;
@@ -120,12 +119,7 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
120119 if ( scope . clearSelected ) {
121120 scope . searchStr = null ;
122121 }
123- scope . showDropdown = false ;
124- scope . results = [ ] ;
125- }
126-
127- function isNewSearchNeeded ( newTerm , oldTerm ) {
128- return newTerm . length >= minlength && newTerm !== oldTerm ;
122+ clearResults ( ) ;
129123 }
130124
131125 function extractTitle ( data ) {
@@ -179,12 +173,8 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
179173 else {
180174 if ( ! scope . searchStr || scope . searchStr === '' ) {
181175 scope . showDropdown = false ;
182- lastSearchTerm = null ;
183- } else if ( isNewSearchNeeded ( scope . searchStr , lastSearchTerm ) ) {
184- lastSearchTerm = scope . searchStr ;
185- scope . showDropdown = true ;
186- scope . currentIndex = - 1 ;
187- scope . results = [ ] ;
176+ } else if ( scope . searchStr . length >= minlength ) {
177+ initResults ( ) ;
188178
189179 if ( searchTimer ) {
190180 $timeout . cancel ( searchTimer ) ;
@@ -294,6 +284,17 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
294284 . error ( httpErrorCallback ) ;
295285 }
296286
287+ function clearResults ( ) {
288+ scope . showDropdown = false ;
289+ scope . results = [ ] ;
290+ }
291+
292+ function initResults ( ) {
293+ scope . showDropdown = true ;
294+ scope . currentIndex = - 1 ;
295+ scope . results = [ ] ;
296+ }
297+
297298 function getLocalResults ( str ) {
298299 var i , match , s ,
299300 searchFields = scope . searchFields . split ( ',' ) ,
@@ -394,13 +395,21 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
394395 scope . searchStr = null ;
395396 }
396397 else {
397- scope . searchStr = lastSearchTerm = result . title ;
398+ scope . searchStr = result . title ;
398399 }
399400 callOrAssign ( result ) ;
400- scope . showDropdown = false ;
401- scope . results = [ ] ;
401+ clearResults ( ) ;
402402 } ;
403403
404+ scope . inputChangeHandler = function ( str ) {
405+ if ( str . length < minlength ) {
406+ clearResults ( ) ;
407+ }
408+ if ( scope . inputChanged ) {
409+ str = scope . inputChanged ( str ) ;
410+ }
411+ return str ;
412+ } ;
404413
405414 // check required
406415 if ( scope . fieldRequiredClass && scope . fieldRequiredClass !== '' ) {
@@ -449,9 +458,6 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$q', '$pa
449458
450459 // set response formatter
451460 responseFormatter = callFunctionOrIdentity ( 'remoteUrlResponseFormatter' ) ;
452-
453- // set response formatter
454- scope . inputChangeHandler = callFunctionOrIdentity ( 'inputChanged' ) ;
455461 }
456462 } ;
457463} ] ) ;
0 commit comments