Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 08aad22

Browse files
committed
revert: set isOptionvalid flag during writting the value (to try another impllementation )
1 parent b1946fd commit 08aad22

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

src/ng/directive/ngOptions.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
414414
var selectCtrl = ctrls[0];
415415
var ngModelCtrl = ctrls[1];
416416
var multiple = attr.multiple;
417-
var isOptionValid = true;
418417

419418
// The emptyOption allows the application developer to provide their own custom "empty"
420419
// option when the viewValue does not match any of the option values.
@@ -467,7 +466,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
467466

468467
selectCtrl.writeValue = function writeNgOptionsValue(value) {
469468
var option = options.getOptionFromViewValue(value);
470-
isOptionValid = option ? true : false;
469+
471470
if (option && !option.disabled) {
472471
if (selectElement[0].value !== option.selectValue) {
473472
removeUnknownOption();
@@ -517,16 +516,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
517516
});
518517

519518
if (value) {
520-
var matchedOptions = 0;
521519
value.forEach(function(item) {
522520
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;
527522
});
528-
529-
isOptionValid = (matchedOptions > 0) ? true : false;
530523
}
531524
};
532525

@@ -560,11 +553,41 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
560553
}
561554
}
562555

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
564584
var $$isEmpty = ngModelCtrl.$isEmpty;
565585

566586
ngModelCtrl.$isEmpty = function(value) {
567-
return $$isEmpty(value) || !isOptionValid;
587+
if ($$isEmpty(value)) {
588+
return true;
589+
}
590+
return !isViewOptionValid(value);
568591
};
569592

570593
if (providedEmptyOption) {
@@ -739,7 +762,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
739762
ngModelCtrl.$render();
740763

741764
// Check to see if the value has changed due to the update to the options
742-
if (!$$isEmpty(previousValue)) {
765+
if (!ngModelCtrl.$isEmpty(previousValue)) {
743766
var nextValue = selectCtrl.readValue();
744767
var isNotPrimitive = ngOptions.trackBy || multiple;
745768
if (isNotPrimitive ? !equals(previousValue, nextValue) : previousValue !== nextValue) {

0 commit comments

Comments
 (0)