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