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

Commit 5cb015b

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 5cb015b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/ng/directive/select.js

+2-3
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

+26
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,32 @@ 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+
compile('<select ng-model="someModel" ng-options="o as o for o in ::arr"></select>');
219+
options = element.find('option');
220+
expect(options.length).toEqual(1);
221+
222+
scope.arr = ['a','b','c'];
223+
scope.$digest();
224+
options = element.find('option');
225+
expect(options.length).toEqual(4);
226+
expect(options.eq(0)).toEqualOption('?', '');
227+
expect(options.eq(1)).toEqualOption('0', 'a');
228+
expect(options.eq(2)).toEqualOption('1', 'b');
229+
expect(options.eq(3)).toEqualOption('2', 'c');
230+
231+
scope.arr = ['w', 'x', 'y', 'z'];
232+
scope.$digest();
233+
options = element.find('option');
234+
expect(options.length).toEqual(4);
235+
expect(options.eq(0)).toEqualOption('?', '');
236+
expect(options.eq(1)).toEqualOption('0', 'a');
237+
expect(options.eq(2)).toEqualOption('1', 'b');
238+
expect(options.eq(3)).toEqualOption('2', 'c');
239+
});
240+
215241
describe('empty option', function() {
216242

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

0 commit comments

Comments
 (0)