@@ -366,67 +366,70 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
366366
367367
368368 selectCtrl . writeValue = function writeNgOptionsValue ( value ) {
369- if ( multiple ) {
369+ var option = options . getOptionFromViewValue ( value ) ;
370370
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 ( ) ;
374375
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' ) ;
380379 }
381-
382380 } 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 ( ) ;
394384 } else {
395- if ( value === null || providedEmptyOption ) {
396- removeUnknownOption ( ) ;
397- renderEmptyOption ( ) ;
398- } else {
399- removeEmptyOption ( ) ;
400- renderUnknownOption ( ) ;
401- }
385+ removeEmptyOption ( ) ;
386+ renderUnknownOption ( ) ;
402387 }
403388 }
404389 } ;
405390
406-
407391 selectCtrl . readValue = function readNgOptionsValue ( ) {
408392
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 ( ) ] ;
417394
418- var option = options . selectValueMap [ selectElement . val ( ) ] ;
395+ if ( selectedOption ) {
419396 removeEmptyOption ( ) ;
420397 removeUnknownOption ( ) ;
421- return option ? option . viewValue : null ;
398+ return selectedOption . viewValue ;
422399 }
400+ return null ;
423401 } ;
424402
425403
404+ // Update the controller methods for multiple selectable options
426405 if ( multiple ) {
406+
427407 ngModelCtrl . $isEmpty = function ( value ) {
428408 return ! value || value . length === 0 ;
429409 } ;
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+ } ;
430433 }
431434
432435
@@ -591,7 +594,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
591594 ngModelCtrl . $render ( ) ;
592595
593596 // 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 ) ) {
595598 var nextValue = selectCtrl . readValue ( ) ;
596599 if ( ! equals ( previousValue , nextValue ) ) {
597600 ngModelCtrl . $setViewValue ( nextValue ) ;
0 commit comments