@@ -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 ( 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
} ;
@@ -552,42 +582,11 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
552
582
553
583
}
554
584
}
555
-
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
585
// Copy the implementation of $isEmpty function to be used in overwritten one
584
586
var $$isEmpty = ngModelCtrl . $isEmpty ;
585
587
586
588
ngModelCtrl . $isEmpty = function ( value ) {
587
- if ( $$isEmpty ( value ) ) {
588
- return true ;
589
- }
590
- return ! isViewOptionValid ( value ) ;
589
+ return $$isEmpty ( value ) || ! options . isSelectedOptionValid ( ) ;
591
590
} ;
592
591
593
592
if ( providedEmptyOption ) {
@@ -762,7 +761,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
762
761
ngModelCtrl . $render ( ) ;
763
762
764
763
// Check to see if the value has changed due to the update to the options
765
- if ( ! ngModelCtrl . $isEmpty ( previousValue ) ) {
764
+ if ( ! $ $isEmpty( previousValue ) ) {
766
765
var nextValue = selectCtrl . readValue ( ) ;
767
766
var isNotPrimitive = ngOptions . trackBy || multiple ;
768
767
if ( isNotPrimitive ? ! equals ( previousValue , nextValue ) : previousValue !== nextValue ) {
0 commit comments