@@ -92,8 +92,10 @@ angular.module('ui.select', [])
92
92
93
93
var ctrl = this ;
94
94
95
+ var EMPTY_SEARCH = '' ;
96
+
95
97
ctrl . placeholder = undefined ;
96
- ctrl . search = undefined ;
98
+ ctrl . search = EMPTY_SEARCH ;
97
99
ctrl . activeIndex = 0 ;
98
100
ctrl . items = [ ] ;
99
101
ctrl . selected = undefined ;
@@ -113,12 +115,38 @@ angular.module('ui.select', [])
113
115
}
114
116
} ;
115
117
116
- ctrl . getItemsAsync = function ( repeatAttr ) {
118
+ var _repeatRhsIsCollection = null ;
119
+ var _repeatRhsFn = null ;
120
+
121
+ ctrl . parseRepeatAttr = function ( repeatAttr ) {
117
122
var repeat = RepeatParser . parse ( repeatAttr ) ;
118
- var getter = $parse ( repeat . rhs ) ;
119
- $q . when ( getter ( $scope ) ) . then ( function ( items ) {
120
- ctrl . items = items ;
121
- } ) ;
123
+ _repeatRhsFn = $parse ( repeat . rhs ) ;
124
+
125
+ var collectionOrPromise = _repeatRhsFn ( $scope ) ;
126
+
127
+ // Hackish :/
128
+ // Determine if the repeat expression (repeat.rhs) gives us a collection or a promise
129
+ // If it is a collection we need to $watch it in order to update ctrl.items
130
+ _repeatRhsIsCollection = angular . isArray ( collectionOrPromise ) ;
131
+
132
+ if ( _repeatRhsIsCollection ) {
133
+ // See https://github.com/angular/angular.js/blob/55848a9139/src/ng/directive/ngRepeat.js#L259
134
+ $scope . $watchCollection ( repeat . rhs , function ( items ) {
135
+ ctrl . items = items ;
136
+ } ) ;
137
+ }
138
+ } ;
139
+
140
+ ctrl . populateItems = function ( ) {
141
+ if ( ! _repeatRhsIsCollection ) {
142
+ var promise = _repeatRhsFn ( $scope ) ;
143
+
144
+ // See https://github.com/angular-ui/bootstrap/blob/d0024931de/src/typeahead/typeahead.js#L109
145
+ // See https://github.com/mgcrea/angular-strap/blob/1529ab4bbc/src/helpers/parse-options.js#L35
146
+ $q . when ( promise ) . then ( function ( items ) {
147
+ ctrl . items = items ;
148
+ } ) ;
149
+ }
122
150
} ;
123
151
124
152
// When the user clicks on an item inside the dropdown list
@@ -129,8 +157,14 @@ angular.module('ui.select', [])
129
157
} ;
130
158
131
159
ctrl . close = function ( ) {
132
- ctrl . open = false ;
133
- ctrl . search = '' ;
160
+ if ( ctrl . open ) {
161
+ ctrl . open = false ;
162
+ if ( _repeatRhsIsCollection ) {
163
+ // This means if repeat.rhs is a promise, we keep the search term (ctrl.search)
164
+ // even after the dropdown being closed
165
+ ctrl . search = EMPTY_SEARCH ;
166
+ }
167
+ }
134
168
} ;
135
169
136
170
var Key = {
@@ -289,9 +323,11 @@ angular.module('ui.select', [])
289
323
. attr ( 'ng-click' , '$select.select(' + repeat . lhs + ')' ) ;
290
324
291
325
return function link ( scope , element , attrs , $select ) {
326
+ $select . parseRepeatAttr ( attrs . repeat ) ;
327
+
292
328
scope . $watch ( '$select.search' , function ( ) {
293
329
$select . activeIndex = 0 ;
294
- $select . getItemsAsync ( attrs . repeat ) ;
330
+ $select . populateItems ( attrs . repeat ) ;
295
331
} ) ;
296
332
} ;
297
333
}
0 commit comments