@@ -387,6 +387,34 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
387
387
selectValueMap [ selectValue ] = optionItem ;
388
388
}
389
389
390
+ /**
391
+ *
392
+ * @returns {boolean }
393
+ */
394
+ function isSelectedOptionValid ( ) {
395
+
396
+ var selectedValue = selectElement . val ( ) ;
397
+
398
+ if ( ! selectedValue ) {
399
+ return false ;
400
+ }
401
+
402
+ // in case select is multiple then check
403
+ // if one of the selected values matched with the options return true
404
+ // or return false if no value match
405
+ if ( angular . isArray ( selectedValue ) ) {
406
+ for ( var i = 0 , length = selectedValue . length ; i < length ; i ++ ) {
407
+ if ( selectValueMap [ selectedValue [ i ] ] ) {
408
+ return true
409
+ }
410
+ }
411
+ return false
412
+ }
413
+
414
+ return ! ! selectValueMap [ selectedValue ] ;
415
+
416
+ }
417
+
390
418
return {
391
419
items : optionItems ,
392
420
selectValueMap : selectValueMap ,
@@ -397,7 +425,9 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
397
425
// If the viewValue could be an object that may be mutated by the application,
398
426
// we need to make a copy and not return the reference to the value on the option.
399
427
return trackBy ? angular . copy ( option . viewValue ) : option . viewValue ;
400
- }
428
+ } ,
429
+
430
+ isSelectedOptionValid : isSelectedOptionValid
401
431
} ;
402
432
}
403
433
} ;
@@ -584,10 +614,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
584
614
var $$isEmpty = ngModelCtrl . $isEmpty ;
585
615
586
616
ngModelCtrl . $isEmpty = function ( value ) {
587
- if ( $$isEmpty ( value ) ) {
588
- return true ;
589
- }
590
- return ! isViewOptionValid ( value ) ;
617
+ return $$isEmpty ( value ) || ! options . isSelectedOptionValid ( ) ;
591
618
} ;
592
619
593
620
if ( providedEmptyOption ) {
@@ -762,7 +789,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762
789
ngModelCtrl . $render ( ) ;
763
790
764
791
// Check to see if the value has changed due to the update to the options
765
- if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
792
+ if ( ! $ $isEmpty( previousValue ) ) {
766
793
var nextValue = selectCtrl . readValue ( ) ;
767
794
var isNotPrimitive = ngOptions . trackBy || multiple ;
768
795
if ( isNotPrimitive ? ! equals ( previousValue , nextValue ) : previousValue !== nextValue ) {
0 commit comments