@@ -414,6 +414,7 @@ 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 ;
417
418
418
419
// The emptyOption allows the application developer to provide their own custom "empty"
419
420
// option when the viewValue does not match any of the option values.
@@ -466,7 +467,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
466
467
467
468
selectCtrl . writeValue = function writeNgOptionsValue ( value ) {
468
469
var option = options . getOptionFromViewValue ( value ) ;
469
-
470
+ isOptionValid = option ? true : false ;
470
471
if ( option && ! option . disabled ) {
471
472
if ( selectElement [ 0 ] . value !== option . selectValue ) {
472
473
removeUnknownOption ( ) ;
@@ -516,10 +517,16 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
516
517
} ) ;
517
518
518
519
if ( value ) {
520
+ var matchedOptions = 0 ;
519
521
value . forEach ( function ( item ) {
520
522
var option = options . getOptionFromViewValue ( item ) ;
521
- if ( option && ! option . disabled ) option . element . selected = true ;
523
+ if ( option && ! option . disabled ) {
524
+ ++ matchedOptions ;
525
+ option . element . selected = true ;
526
+ }
522
527
} ) ;
528
+
529
+ isOptionValid = ( matchedOptions > 0 ) ? true : false ;
523
530
}
524
531
} ;
525
532
@@ -553,41 +560,11 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
553
560
}
554
561
}
555
562
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
563
+ // Copy the implementation of $isEmpty function to be used in overwritten one.
584
564
var $$isEmpty = ngModelCtrl . $isEmpty ;
585
565
586
566
ngModelCtrl . $isEmpty = function ( value ) {
587
- if ( $$isEmpty ( value ) ) {
588
- return true ;
589
- }
590
- return ! isViewOptionValid ( value ) ;
567
+ return $$isEmpty ( value ) || ! isOptionValid ;
591
568
} ;
592
569
593
570
if ( providedEmptyOption ) {
@@ -762,7 +739,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762
739
ngModelCtrl . $render ( ) ;
763
740
764
741
// Check to see if the value has changed due to the update to the options
765
- if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
742
+ if ( ! $ $isEmpty( previousValue ) ) {
766
743
var nextValue = selectCtrl . readValue ( ) ;
767
744
var isNotPrimitive = ngOptions . trackBy || multiple ;
768
745
if ( isNotPrimitive ? ! equals ( previousValue , nextValue ) : previousValue !== nextValue ) {
0 commit comments