@@ -239,8 +239,19 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
239
239
$scope . searchText = val ;
240
240
} ) ;
241
241
}
242
- if ( angular . isFunction ( $scope . itemChange ) && selectedItem !== previousSelectedItem )
243
- $scope . itemChange ( getItemScope ( selectedItem ) ) ;
242
+
243
+ if ( selectedItem !== previousSelectedItem ) announceItemChange ( selectedItem ) ;
244
+ }
245
+
246
+ /**
247
+ * Use the user-defined expression to announce changes each time a new item is selected
248
+ */
249
+ function announceItemChange ( current ) {
250
+ angular . isFunction ( $scope . itemChange ) && $scope . itemChange ( getItemAsNameVal ( current ) ) ;
251
+ }
252
+
253
+ function announceTextChange ( value ) {
254
+ angular . isFunction ( $scope . textChange ) && $scope . textChange ( value ) ;
244
255
}
245
256
246
257
/**
@@ -283,25 +294,27 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
283
294
*/
284
295
function handleSearchText ( searchText , previousSearchText ) {
285
296
ctrl . index = getDefaultIndex ( ) ;
286
- //-- do nothing on init
297
+ // do nothing on init
287
298
if ( searchText === previousSearchText ) return ;
288
299
289
300
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 ( ) ;
301
+ // clear selected item if search text no longer matches it
302
+ if ( searchText !== val )
303
+ {
304
+ $scope . selectedItem = null ;
305
+
306
+ // trigger change event if available
307
+ if ( searchText !== previousSearchText ) announceTextChange ( searchText ) ;
308
+
309
+ // cancel results if search text is not long enough
310
+ if ( ! isMinLengthMet ( ) ) {
311
+ ctrl . loading = false ;
312
+ ctrl . matches = [ ] ;
313
+ ctrl . hidden = shouldHide ( ) ;
314
+ updateMessages ( ) ;
315
+ } else {
316
+ handleQuery ( ) ;
317
+ }
305
318
}
306
319
} ) ;
307
320
@@ -378,21 +391,34 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
378
391
* @returns {* }
379
392
*/
380
393
function getDisplayValue ( item ) {
381
- return ( item && $scope . itemText ) ? $q . when ( $scope . itemText ( getItemScope ( item ) ) ) : $q . when ( item ) ;
394
+ return $q . when ( getItemText ( item ) || item ) ;
395
+
396
+ /**
397
+ * Getter function to invoke user-defined expression (in the directive)
398
+ * to convert your object to a single string.
399
+ */
400
+ function getItemText ( item ) {
401
+ return ( item && $scope . itemText ) ? $scope . itemText ( getItemAsNameVal ( item ) ) : null ;
402
+ }
382
403
}
383
404
384
405
/**
385
406
* Returns the locals object for compiling item templates.
386
407
* @param item
387
408
* @returns {{} }
388
409
*/
389
- function getItemScope ( item ) {
390
- if ( ! item ) return ;
410
+ function getItemAsNameVal ( item ) {
411
+ if ( ! item ) return undefined ;
412
+
391
413
var locals = { } ;
392
414
if ( ctrl . itemName ) locals [ ctrl . itemName ] = item ;
415
+
393
416
return locals ;
394
417
}
395
418
419
+
420
+
421
+
396
422
/**
397
423
* Returns the default index based on whether or not autoselect is enabled.
398
424
* @returns {number }
@@ -414,7 +440,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $timeout, $
414
440
* @returns {* }
415
441
*/
416
442
function getCurrentDisplayValue ( ) {
417
- return getDisplayValue ( ctrl . matches [ ctrl . index ] ) ;
443
+ return getDisplayValue ( ctrl . matches [ ctrl . index ] ) ;
418
444
}
419
445
420
446
/**
0 commit comments