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

Commit a87910f

Browse files
committed
fix(ng-options): support one-time binding on the options
Preserve the $watchDelegate on the watcher used to detect changes on the labels Closes #10687
1 parent f3b088a commit a87910f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/ng/directive/select.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
414414
ctrl.$render = render;
415415

416416
scope.$watchCollection(valuesFn, scheduleRendering);
417-
scope.$watchCollection(getLabels, scheduleRendering);
417+
scope.$watchCollection($parse(valuesFn, getLabels), scheduleRendering);
418418

419419
if (multiple) {
420420
scope.$watchCollection(function() { return ctrl.$modelValue; }, scheduleRendering);
@@ -458,8 +458,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
458458
}
459459
}
460460

461-
function getLabels() {
462-
var values = valuesFn(scope);
461+
function getLabels(values) {
463462
var toDisplay;
464463
if (values && isArray(values)) {
465464
toDisplay = new Array(values.length);

test/ng/directive/selectSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,29 @@ describe('select', function() {
212212
});
213213

214214

215+
it('should be possible to use one-time binding on the expression', function() {
216+
var options;
217+
218+
scope.arr = ['a','b','c'];
219+
compile('<select ng-model="someModel" ng-options="o as o for o in ::arr"></select>');
220+
options = element.find('option');
221+
222+
expect(options.length).toEqual(4);
223+
expect(options.eq(0)).toEqualOption('?', '');
224+
expect(options.eq(1)).toEqualOption('0', 'a');
225+
expect(options.eq(2)).toEqualOption('1', 'b');
226+
expect(options.eq(3)).toEqualOption('2', 'c');
227+
228+
scope.arr = ['w', 'x', 'y', 'z'];
229+
scope.$digest();
230+
options = element.find('option');
231+
expect(options.length).toEqual(4);
232+
expect(options.eq(0)).toEqualOption('?', '');
233+
expect(options.eq(1)).toEqualOption('0', 'a');
234+
expect(options.eq(2)).toEqualOption('1', 'b');
235+
expect(options.eq(3)).toEqualOption('2', 'c');
236+
});
237+
215238
describe('empty option', function() {
216239

217240
it('should select the empty option when model is undefined', function() {

0 commit comments

Comments
 (0)