@@ -37,7 +37,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
37
37
require :'ngModel' ,
38
38
link :function ( originalScope , element , attrs , modelCtrl ) {
39
39
40
- var selected ;
40
+ var $setModelValue = $parse ( attrs . ngModel ) . assign ;
41
41
42
42
//minimal no of characters that needs to be entered before typeahead kicks-in
43
43
var minSearch = originalScope . $eval ( attrs . typeaheadMinLength ) || 1 ;
@@ -130,48 +130,47 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
130
130
var timeoutId ;
131
131
132
132
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
146
137
}
138
+ timeoutId = $timeout ( function ( ) {
139
+ getMatchesAsync ( inputValue ) ;
140
+ } , waitTime ) ;
141
+ } else {
142
+ getMatchesAsync ( inputValue ) ;
147
143
}
148
144
}
149
145
150
146
return isEditable ? inputValue : undefined ;
151
147
} ) ;
152
148
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
+ } ) ;
159
157
160
158
scope . select = function ( activeIdx ) {
161
159
//called from within the $digest() cycle
162
160
var locals = { } ;
163
161
var model , item ;
164
- locals [ parserResult . itemName ] = item = selected = scope . matches [ activeIdx ] . model ;
165
162
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 , {
170
168
$item : item ,
171
169
$model : model ,
172
- $label : parserResult . viewMapper ( scope , locals )
170
+ $label : parserResult . viewMapper ( originalScope , locals )
173
171
} ) ;
174
172
173
+ //return focus to the input element if a mach was selected via a mouse click event
175
174
element [ 0 ] . focus ( ) ;
176
175
} ;
177
176
@@ -259,4 +258,4 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
259
258
return function ( matchItem , query ) {
260
259
return query ? matchItem . replace ( new RegExp ( escapeRegexp ( query ) , 'gi' ) , '<strong>$&</strong>' ) : query ;
261
260
} ;
262
- } ) ;
261
+ } ) ;
0 commit comments