1
- angular . module ( 'ui.bootstrap.typeahead' , [ ] )
1
+ angular . module ( 'ui.bootstrap.typeahead' , [ 'ui.bootstrap.position' ] )
2
2
3
3
/**
4
4
* A helper service that can parse typeahead's syntax (string provided by users)
@@ -29,8 +29,7 @@ angular.module('ui.bootstrap.typeahead', [])
29
29
} ;
30
30
} ] )
31
31
32
- //options - min length
33
- . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$document' , 'typeaheadParser' , function ( $compile , $parse , $q , $document , typeaheadParser ) {
32
+ . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$document' , '$position' , 'typeaheadParser' , function ( $compile , $parse , $q , $document , $position , typeaheadParser ) {
34
33
35
34
var HOT_KEYS = [ 9 , 13 , 27 , 38 , 40 ] ;
36
35
@@ -51,6 +50,16 @@ angular.module('ui.bootstrap.typeahead', [])
51
50
52
51
var isLoadingSetter = $parse ( attrs . typeaheadLoading ) . assign || angular . noop ;
53
52
53
+ //pop-up element used to display matches
54
+ var popUpEl = angular . element (
55
+ "<typeahead-popup " +
56
+ "matches='matches' " +
57
+ "active='activeIdx' " +
58
+ "select='select(activeIdx)' " +
59
+ "query='query' " +
60
+ "position='position'>" +
61
+ "</typeahead-popup>" ) ;
62
+
54
63
//create a child scope for the typeahead directive so we are not polluting original scope
55
64
//with typeahead-specific data (matches, query etc.)
56
65
var scope = originalScope . $new ( ) ;
@@ -87,6 +96,11 @@ angular.module('ui.bootstrap.typeahead', [])
87
96
}
88
97
89
98
scope . query = inputValue ;
99
+ //position pop-up with matches - we need to re-calculate its position each time we are opening a window
100
+ //with matches as a pop-up might be absolute-positioned and position of an input might have changed on a page
101
+ //due to other elements being rendered
102
+ scope . position = $position . position ( element ) ;
103
+ scope . position . top = scope . position . top + element . prop ( 'offsetHeight' ) ;
90
104
91
105
} else {
92
106
resetMatches ( ) ;
@@ -167,15 +181,12 @@ angular.module('ui.bootstrap.typeahead', [])
167
181
}
168
182
} ) ;
169
183
170
- $document . find ( 'body' ) . bind ( 'click' , function ( ) {
171
-
184
+ $document . bind ( 'click' , function ( ) {
172
185
resetMatches ( ) ;
173
186
scope . $digest ( ) ;
174
187
} ) ;
175
188
176
- var tplElCompiled = $compile ( "<typeahead-popup matches='matches' active='activeIdx' select='select(activeIdx)' " +
177
- "query='query'></typeahead-popup>" ) ( scope ) ;
178
- element . after ( tplElCompiled ) ;
189
+ element . after ( $compile ( popUpEl ) ( scope ) ) ;
179
190
}
180
191
} ;
181
192
@@ -188,6 +199,7 @@ angular.module('ui.bootstrap.typeahead', [])
188
199
matches :'=' ,
189
200
query :'=' ,
190
201
active :'=' ,
202
+ position :'=' ,
191
203
select :'&'
192
204
} ,
193
205
replace :true ,
0 commit comments