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

Commit 555f415

Browse files
committed
fix(ng:options): fix selecting options
Contains 3 fixes: - the internal model was by mistake using "checked" property instead of "selected" - use jqLite.prop() to set 'selected' property - added inChangeEvent check - we should not interfere with the browser selecting elements when not necessary
1 parent 3800d17 commit 555f415

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/widgets.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ angularWidget('select', function(element){
716716
// optionGroupsCache[?][0] is the parent: either the SELECT or OPTGROUP element
717717
var optionGroupsCache = [[{element: selectElement, label:''}]],
718718
scope = this,
719-
model = modelAccessor(scope, element);
719+
model = modelAccessor(scope, element),
720+
inChangeEvent;
720721

721722
// find existing special options
722723
forEach(selectElement.children(), function(option){
@@ -733,6 +734,12 @@ angularWidget('select', function(element){
733734
tempScope = scope.$new(),
734735
value, optionElement, index, groupIndex, length, groupLength;
735736

737+
// let's set a flag that the current model change is due to a change event.
738+
// the default action of option selection will cause the appropriate option element to be
739+
// deselected and another one to be selected - there is no need for us to be updating the DOM
740+
// in this case.
741+
inChangeEvent = true;
742+
736743
try {
737744
if (isMultiselect) {
738745
value = [];
@@ -768,6 +775,7 @@ angularWidget('select', function(element){
768775
scope.$root.$apply();
769776
} finally {
770777
tempScope = null; // TODO(misko): needs to be $destroy
778+
inChangeEvent = false;
771779
}
772780
});
773781

@@ -886,8 +894,8 @@ angularWidget('select', function(element){
886894
if (existingOption.id !== option.id) {
887895
lastElement.val(existingOption.id = option.id);
888896
}
889-
if (existingOption.selected !== option.selected) {
890-
lastElement.attr('selected', option.selected);
897+
if (!inChangeEvent && existingOption.selected !== option.selected) {
898+
lastElement.prop('selected', (existingOption.selected = option.selected));
891899
}
892900
} else {
893901
// grow elements
@@ -902,7 +910,7 @@ angularWidget('select', function(element){
902910
element: element,
903911
label: option.label,
904912
id: option.id,
905-
checked: option.selected
913+
selected: option.selected
906914
});
907915
if (lastElement) {
908916
lastElement.after(element);

0 commit comments

Comments
 (0)