@@ -37,7 +37,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
3737 require :'ngModel' ,
3838 link :function ( originalScope , element , attrs , modelCtrl ) {
3939
40- var selected ;
40+ var $setModelValue = $parse ( attrs . ngModel ) . assign ;
4141
4242 //minimal no of characters that needs to be entered before typeahead kicks-in
4343 var minSearch = originalScope . $eval ( attrs . typeaheadMinLength ) || 1 ;
@@ -130,48 +130,47 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
130130 var timeoutId ;
131131
132132 resetMatches ( ) ;
133- if ( selected ) {
134- return inputValue ;
135- } else {
136- if ( inputValue && inputValue . length >= minSearch ) {
137- if ( waitTime > 0 ) {
138- if ( timeoutId ) {
139- $timeout . cancel ( timeoutId ) ; //cancel previous timeout
140- }
141- timeoutId = $timeout ( function ( ) {
142- getMatchesAsync ( inputValue ) ;
143- } , waitTime ) ;
144- } else {
145- getMatchesAsync ( inputValue ) ;
133+ if ( inputValue && inputValue . length >= minSearch ) {
134+ if ( waitTime > 0 ) {
135+ if ( timeoutId ) {
136+ $timeout . cancel ( timeoutId ) ; //cancel previous timeout
146137 }
138+ timeoutId = $timeout ( function ( ) {
139+ getMatchesAsync ( inputValue ) ;
140+ } , waitTime ) ;
141+ } else {
142+ getMatchesAsync ( inputValue ) ;
147143 }
148144 }
149145
150146 return isEditable ? inputValue : undefined ;
151147 } ) ;
152148
153- modelCtrl . $render = function ( ) {
154- var locals = { } ;
155- locals [ parserResult . itemName ] = selected || modelCtrl . $viewValue ;
156- element . val ( parserResult . viewMapper ( scope , locals ) || modelCtrl . $viewValue ) ;
157- selected = undefined ;
158- } ;
149+ modelCtrl . $formatters . push ( function ( modelValue ) {
150+ var locals = { } , viewValue ;
151+ locals [ parserResult . itemName ] = modelValue ;
152+
153+ viewValue = parserResult . viewMapper ( originalScope , locals ) ;
154+
155+ return viewValue ? viewValue : modelValue ;
156+ } ) ;
159157
160158 scope . select = function ( activeIdx ) {
161159 //called from within the $digest() cycle
162160 var locals = { } ;
163161 var model , item ;
164- locals [ parserResult . itemName ] = item = selected = scope . matches [ activeIdx ] . model ;
165162
166- model = parserResult . modelMapper ( scope , locals ) ;
167- modelCtrl . $setViewValue ( model ) ;
168- modelCtrl . $render ( ) ;
169- onSelectCallback ( scope , {
163+ locals [ parserResult . itemName ] = item = scope . matches [ activeIdx ] . model ;
164+ model = parserResult . modelMapper ( originalScope , locals ) ;
165+ $setModelValue ( originalScope , model ) ;
166+
167+ onSelectCallback ( originalScope , {
170168 $item : item ,
171169 $model : model ,
172- $label : parserResult . viewMapper ( scope , locals )
170+ $label : parserResult . viewMapper ( originalScope , locals )
173171 } ) ;
174172
173+ //return focus to the input element if a mach was selected via a mouse click event
175174 element [ 0 ] . focus ( ) ;
176175 } ;
177176
@@ -259,4 +258,4 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
259258 return function ( matchItem , query ) {
260259 return query ? matchItem . replace ( new RegExp ( escapeRegexp ( query ) , 'gi' ) , '<strong>$&</strong>' ) : query ;
261260 } ;
262- } ) ;
261+ } ) ;
0 commit comments