@@ -366,67 +366,70 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
366
366
367
367
368
368
selectCtrl . writeValue = function writeNgOptionsValue ( value ) {
369
- if ( multiple ) {
369
+ var option = options . getOptionFromViewValue ( value ) ;
370
370
371
- options . items . forEach ( function ( option ) {
372
- option . element . selected = false ;
373
- } ) ;
371
+ if ( option ) {
372
+ if ( selectElement [ 0 ] . value !== option . selectValue ) {
373
+ removeUnknownOption ( ) ;
374
+ removeEmptyOption ( ) ;
374
375
375
- if ( value ) {
376
- value . forEach ( function ( item ) {
377
- var option = options . getOptionFromViewValue ( item ) ;
378
- if ( option ) option . element . selected = true ;
379
- } ) ;
376
+ selectElement [ 0 ] . value = option . selectValue ;
377
+ option . element . selected = true ;
378
+ option . element . setAttribute ( 'selected' , 'selected' ) ;
380
379
}
381
-
382
380
} else {
383
- var option = options . getOptionFromViewValue ( value ) ;
384
-
385
- if ( option ) {
386
- if ( selectElement [ 0 ] . value !== option . selectValue ) {
387
- removeUnknownOption ( ) ;
388
- removeEmptyOption ( ) ;
389
-
390
- selectElement [ 0 ] . value = option . selectValue ;
391
- option . element . selected = true ;
392
- option . element . setAttribute ( 'selected' , 'selected' ) ;
393
- }
381
+ if ( value === null || providedEmptyOption ) {
382
+ removeUnknownOption ( ) ;
383
+ renderEmptyOption ( ) ;
394
384
} else {
395
- if ( value === null || providedEmptyOption ) {
396
- removeUnknownOption ( ) ;
397
- renderEmptyOption ( ) ;
398
- } else {
399
- removeEmptyOption ( ) ;
400
- renderUnknownOption ( ) ;
401
- }
385
+ removeEmptyOption ( ) ;
386
+ renderUnknownOption ( ) ;
402
387
}
403
388
}
404
389
} ;
405
390
406
-
407
391
selectCtrl . readValue = function readNgOptionsValue ( ) {
408
392
409
- if ( multiple ) {
410
-
411
- return selectElement . val ( ) . map ( function ( selectedKey ) {
412
- var option = options . selectValueMap [ selectedKey ] ;
413
- return option . viewValue ;
414
- } ) ;
415
-
416
- } else {
393
+ var selectedOption = options . selectValueMap [ selectElement . val ( ) ] ;
417
394
418
- var option = options . selectValueMap [ selectElement . val ( ) ] ;
395
+ if ( selectedOption ) {
419
396
removeEmptyOption ( ) ;
420
397
removeUnknownOption ( ) ;
421
- return option ? option . viewValue : null ;
398
+ return selectedOption . viewValue ;
422
399
}
400
+ return null ;
423
401
} ;
424
402
425
403
404
+ // Update the controller methods for multiple selectable options
426
405
if ( multiple ) {
406
+
427
407
ngModelCtrl . $isEmpty = function ( value ) {
428
408
return ! value || value . length === 0 ;
429
409
} ;
410
+
411
+
412
+ selectCtrl . writeValue = function writeNgOptionsMultiple ( value ) {
413
+ options . items . forEach ( function ( option ) {
414
+ option . element . selected = false ;
415
+ } ) ;
416
+
417
+ if ( value ) {
418
+ value . forEach ( function ( item ) {
419
+ var option = options . getOptionFromViewValue ( item ) ;
420
+ if ( option ) option . element . selected = true ;
421
+ } ) ;
422
+ }
423
+ } ;
424
+
425
+
426
+ selectCtrl . readValue = function readNgOptionsMultiple ( ) {
427
+ var selectedValues = selectElement . val ( ) || [ ] ;
428
+ return selectedValues . map ( function ( selectedKey ) {
429
+ var option = options . selectValueMap [ selectedKey ] ;
430
+ return option . viewValue ;
431
+ } ) ;
432
+ } ;
430
433
}
431
434
432
435
@@ -591,7 +594,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
591
594
ngModelCtrl . $render ( ) ;
592
595
593
596
// Check to see if the value has changed due to the update to the options
594
- if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
597
+ if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
595
598
var nextValue = selectCtrl . readValue ( ) ;
596
599
if ( ! equals ( previousValue , nextValue ) ) {
597
600
ngModelCtrl . $setViewValue ( nextValue ) ;
0 commit comments