@@ -7,7 +7,7 @@ var ITEM_HEIGHT = 41,
7
7
MENU_PADDING = 8 ;
8
8
9
9
function MdAutocompleteCtrl ( $scope , $element , $mdUtil , $mdConstant , $timeout , $mdTheming , $window ,
10
- $animate , $rootElement , $attrs ) {
10
+ $animate , $rootElement , $attrs , $q ) {
11
11
//-- private variables
12
12
var ctrl = this ,
13
13
itemParts = $scope . itemsExpr . split ( / i n / i) ,
@@ -235,7 +235,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
235
235
*/
236
236
function selectedItemChange ( selectedItem , previousSelectedItem ) {
237
237
if ( selectedItem ) {
238
- $scope . searchText = getDisplayValue ( selectedItem ) ;
238
+ getDisplayValue ( selectedItem ) . then ( function ( val ) {
239
+ $scope . searchText = val ;
240
+ } ) ;
239
241
}
240
242
if ( angular . isFunction ( $scope . itemChange ) && selectedItem !== previousSelectedItem )
241
243
$scope . itemChange ( getItemScope ( selectedItem ) ) ;
@@ -283,21 +285,26 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
283
285
ctrl . index = getDefaultIndex ( ) ;
284
286
//-- do nothing on init
285
287
if ( searchText === previousSearchText ) return ;
286
- //-- clear selected item if search text no longer matches it
287
- if ( searchText !== getDisplayValue ( $scope . selectedItem ) ) $scope . selectedItem = null ;
288
- else return ;
289
- //-- trigger change event if available
290
- if ( angular . isFunction ( $scope . textChange ) && searchText !== previousSearchText )
291
- $scope . textChange ( getItemScope ( $scope . selectedItem ) ) ;
292
- //-- cancel results if search text is not long enough
293
- if ( ! isMinLengthMet ( ) ) {
294
- ctrl . loading = false ;
295
- ctrl . matches = [ ] ;
296
- ctrl . hidden = shouldHide ( ) ;
297
- updateMessages ( ) ;
298
- } else {
299
- handleQuery ( ) ;
300
- }
288
+
289
+ getDisplayValue ( $scope . selectedItem ) . then ( function ( val ) {
290
+ //-- clear selected item if search text no longer matches it
291
+ if ( searchText !== val ) $scope . selectedItem = null ;
292
+ else return ;
293
+
294
+ //-- trigger change event if available
295
+ if ( angular . isFunction ( $scope . textChange ) && searchText !== previousSearchText )
296
+ $scope . textChange ( getItemScope ( $scope . selectedItem ) ) ;
297
+ //-- cancel results if search text is not long enough
298
+ if ( ! isMinLengthMet ( ) ) {
299
+ ctrl . loading = false ;
300
+ ctrl . matches = [ ] ;
301
+ ctrl . hidden = shouldHide ( ) ;
302
+ updateMessages ( ) ;
303
+ } else {
304
+ handleQuery ( ) ;
305
+ }
306
+ } ) ;
307
+
301
308
}
302
309
303
310
/**
@@ -371,7 +378,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
371
378
* @returns {* }
372
379
*/
373
380
function getDisplayValue ( item ) {
374
- return ( item && $scope . itemText ) ? $scope . itemText ( getItemScope ( item ) ) : item ;
381
+ return ( item && $scope . itemText ) ? $q . when ( $ scope. itemText ( getItemScope ( item ) ) ) : $q . when ( item ) ;
375
382
}
376
383
377
384
/**
@@ -441,20 +448,24 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
441
448
* Selects the item at the given index.
442
449
* @param index
443
450
*/
444
- function select ( index ) {
445
- $scope . selectedItem = ctrl . matches [ index ] ;
446
- ctrl . loading = false ;
447
- ctrl . hidden = true ;
448
- ctrl . index = 0 ;
449
- ctrl . matches = [ ] ;
451
+ function select ( index ) {
450
452
//-- force form to update state for validation
451
- $timeout ( function ( ) {
452
- elements . $ . input . controller ( 'ngModel' ) . $setViewValue ( getDisplayValue ( $scope . selectedItem ) ||
453
- $scope . searchText ) ;
454
- ctrl . hidden = true ;
453
+ $timeout ( function ( ) {
454
+ getDisplayValue ( ctrl . matches [ index ] ) . then ( function ( val ) {
455
+ var ngModel = elements . $ . input . controller ( 'ngModel' ) ;
456
+ ngModel . $setViewValue ( val ) ;
457
+ ngModel . $render ( ) ;
458
+ } ) . finally ( function ( ) {
459
+ $scope . selectedItem = ctrl . matches [ index ] ;
460
+ ctrl . loading = false ;
461
+ ctrl . hidden = true ;
462
+ ctrl . index = 0 ;
463
+ ctrl . matches = [ ] ;
464
+ } ) ;
455
465
} ) ;
456
466
}
457
467
468
+
458
469
/**
459
470
* Clears the searchText value and selected item.
460
471
*/
@@ -501,7 +512,9 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
501
512
* Updates the ARIA messages
502
513
*/
503
514
function updateMessages ( ) {
504
- ctrl . messages = [ getCountMessage ( ) , getCurrentDisplayValue ( ) ] ;
515
+ getCurrentDisplayValue ( ) . then ( function ( msg ) {
516
+ ctrl . messages = [ getCountMessage ( ) , msg ] ;
517
+ } ) ;
505
518
}
506
519
507
520
/**
0 commit comments