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

Commit 6ecac8e

Browse files
committed
fix(select): multiselect failes to update view on selection insert
In multiselect when the underlying selection array push/pops an element the view did not re-render since the array reference stayed the same.
1 parent 823adb2 commit 6ecac8e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/directive/select.js

+10
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,23 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
174174
}
175175

176176
function Multiple(scope, selectElement, ctrl) {
177+
var lastView;
177178
ctrl.$render = function() {
178179
var items = new HashMap(ctrl.$viewValue);
179180
forEach(selectElement.children(), function(option) {
180181
option.selected = isDefined(items.get(option.value));
181182
});
182183
};
183184

185+
// we have to do it on each watch since ng-model watches reference, but
186+
// we need to work of an array, so we need to see if anything was inserted/removed
187+
scope.$watch(function() {
188+
if (!equals(lastView, ctrl.$viewValue)) {
189+
lastView = copy(ctrl.$viewValue);
190+
ctrl.$render();
191+
}
192+
});
193+
184194
selectElement.bind('change', function() {
185195
scope.$apply(function() {
186196
var array = [];

test/directive/selectSpec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ describe('select', function() {
102102
scope.selection = ['A'];
103103
});
104104

105-
expect(element[0].childNodes[0].selected).toEqual(true);
105+
expect(element.find('option')[0].selected).toEqual(true);
106+
expect(element.find('option')[1].selected).toEqual(false);
107+
108+
scope.$apply(function() {
109+
scope.selection.push('B');
110+
});
111+
112+
expect(element.find('option')[0].selected).toEqual(true);
113+
expect(element.find('option')[1].selected).toEqual(true);
106114
});
107115

108116

0 commit comments

Comments
 (0)