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

Commit ba90261

Browse files
fix(ngOptions): support one-time binding on the option values
Utilize the $watchDelegate on the watcher used to detect changes to the labels. Closes #10687 Closes #10694
1 parent fc21db8 commit ba90261

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/ng/directive/ngOptions.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,12 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
258258
}
259259

260260
return {
261-
getWatchables: function() {
261+
getWatchables: $parse(valuesFn, function(values) {
262262
// Create a collection of things that we would like to watch (watchedArray)
263263
// so that they can all be watched using a single $watchCollection
264264
// that only runs the handler once if anything changes
265265
var watchedArray = [];
266-
267-
var values = valuesFn(scope) || [];
266+
values = values || [];
268267

269268
Object.keys(values).forEach(function getWatchable(key) {
270269
var locals = getLocals(values[key], key);
@@ -274,7 +273,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
274273
watchedArray.push(label);
275274
});
276275
return watchedArray;
277-
},
276+
}),
278277

279278
getOptions: function() {
280279

test/ng/directive/ngOptionsSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,42 @@ describe('ngOptions', function() {
497497
});
498498

499499

500+
501+
it('should be possible to use one-time binding on the expression', function() {
502+
createSelect({
503+
'ng-model': 'someModel',
504+
'ng-options': 'o as o for o in ::arr'
505+
});
506+
507+
var options;
508+
509+
// Initially the options list is just the unknown option
510+
options = element.find('option');
511+
expect(options.length).toEqual(1);
512+
513+
// Now initialize the scope and the options should be updated
514+
scope.$apply(function() {
515+
scope.arr = ['a','b','c'];
516+
});
517+
options = element.find('option');
518+
expect(options.length).toEqual(4);
519+
expect(options.eq(0)).toEqualUnknownOption();
520+
expect(options.eq(1)).toEqualOption('a');
521+
expect(options.eq(2)).toEqualOption('b');
522+
expect(options.eq(3)).toEqualOption('c');
523+
524+
// Change the scope but the options should not change
525+
scope.arr = ['w', 'x', 'y', 'z'];
526+
scope.$digest();
527+
options = element.find('option');
528+
expect(options.length).toEqual(4);
529+
expect(options.eq(0)).toEqualUnknownOption();
530+
expect(options.eq(1)).toEqualOption('a');
531+
expect(options.eq(2)).toEqualOption('b');
532+
expect(options.eq(3)).toEqualOption('c');
533+
});
534+
535+
500536
describe('selectAs expression', function() {
501537
beforeEach(function() {
502538
scope.arr = [{id: 10, label: 'ten'}, {id:20, label: 'twenty'}];

0 commit comments

Comments
 (0)