@@ -414,7 +414,6 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
414
414
var selectCtrl = ctrls [ 0 ] ;
415
415
var ngModelCtrl = ctrls [ 1 ] ;
416
416
var multiple = attr . multiple ;
417
- var isOptionValid = true ;
418
417
419
418
// The emptyOption allows the application developer to provide their own custom "empty"
420
419
// option when the viewValue does not match any of the option values.
@@ -467,7 +466,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
467
466
468
467
selectCtrl . writeValue = function writeNgOptionsValue ( value ) {
469
468
var option = options . getOptionFromViewValue ( value ) ;
470
- isOptionValid = option ? true : false ;
469
+
471
470
if ( option && ! option . disabled ) {
472
471
if ( selectElement [ 0 ] . value !== option . selectValue ) {
473
472
removeUnknownOption ( ) ;
@@ -517,16 +516,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
517
516
} ) ;
518
517
519
518
if ( value ) {
520
- var matchedOptions = 0 ;
521
519
value . forEach ( function ( item ) {
522
520
var option = options . getOptionFromViewValue ( item ) ;
523
- if ( option && ! option . disabled ) {
524
- ++ matchedOptions ;
525
- option . element . selected = true ;
526
- }
521
+ if ( option && ! option . disabled ) option . element . selected = true ;
527
522
} ) ;
528
-
529
- isOptionValid = ( matchedOptions > 0 ) ? true : false ;
530
523
}
531
524
} ;
532
525
@@ -560,11 +553,41 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
560
553
}
561
554
}
562
555
563
- // Copy the implementation of $isEmpty function to be used in overwritten one.
556
+ function isViewOptionValid ( viewValue ) {
557
+
558
+ var isValidOption = false ;
559
+ var viewOptions = [ ] ;
560
+ // Get all option and add them to viewOptions array
561
+ angular . forEach ( options . items , function ( item ) {
562
+ viewOptions . push ( options . getViewValueFromOption ( item ) ) ;
563
+ } ) ;
564
+
565
+ // In case of multiple view is an array so validate all view values
566
+ // if one of them match set isValidOption to true
567
+ if ( multiple ) {
568
+ for ( var i = 0 , length = viewValue . length ; i < length ; i ++ ) {
569
+ if ( viewOptions . indexOf ( viewValue [ i ] ) > - 1 ) {
570
+ isValidOption = true ;
571
+ break ;
572
+ }
573
+ }
574
+ } else {
575
+ if ( viewOptions . indexOf ( viewValue ) > - 1 ) {
576
+ isValidOption = true ;
577
+ }
578
+ }
579
+
580
+ return isValidOption ;
581
+ }
582
+
583
+ // Copy the implementation of $isEmpty function to be used in overwritten one
564
584
var $$isEmpty = ngModelCtrl . $isEmpty ;
565
585
566
586
ngModelCtrl . $isEmpty = function ( value ) {
567
- return $$isEmpty ( value ) || ! isOptionValid ;
587
+ if ( $$isEmpty ( value ) ) {
588
+ return true ;
589
+ }
590
+ return ! isViewOptionValid ( value ) ;
568
591
} ;
569
592
570
593
if ( providedEmptyOption ) {
@@ -739,7 +762,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
739
762
ngModelCtrl . $render ( ) ;
740
763
741
764
// Check to see if the value has changed due to the update to the options
742
- if ( ! $ $isEmpty( previousValue ) ) {
765
+ if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
743
766
var nextValue = selectCtrl . readValue ( ) ;
744
767
var isNotPrimitive = ngOptions . trackBy || multiple ;
745
768
if ( isNotPrimitive ? ! equals ( previousValue , nextValue ) : previousValue !== nextValue ) {
0 commit comments