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

fix(ng-options): support one-time binding on the options #10694

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
ctrl.$render = render;

scope.$watchCollection(valuesFn, scheduleRendering);
scope.$watchCollection(getLabels, scheduleRendering);
scope.$watchCollection($parse(valuesFn, getLabels), scheduleRendering);

if (multiple) {
scope.$watchCollection(function() { return ctrl.$modelValue; }, scheduleRendering);
Expand Down Expand Up @@ -458,8 +458,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
}
}

function getLabels() {
var values = valuesFn(scope);
function getLabels(values) {
var toDisplay;
if (values && isArray(values)) {
toDisplay = new Array(values.length);
Expand Down
26 changes: 26 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ describe('select', function() {
});


it('should be possible to use one-time binding on the expression', function() {
var options;

compile('<select ng-model="someModel" ng-options="o as o for o in ::arr"></select>');
options = element.find('option');
expect(options.length).toEqual(1);

scope.arr = ['a','b','c'];
scope.$digest();
options = element.find('option');
expect(options.length).toEqual(4);
expect(options.eq(0)).toEqualOption('?', '');
expect(options.eq(1)).toEqualOption('0', 'a');
expect(options.eq(2)).toEqualOption('1', 'b');
expect(options.eq(3)).toEqualOption('2', 'c');

scope.arr = ['w', 'x', 'y', 'z'];
scope.$digest();
options = element.find('option');
expect(options.length).toEqual(4);
expect(options.eq(0)).toEqualOption('?', '');
expect(options.eq(1)).toEqualOption('0', 'a');
expect(options.eq(2)).toEqualOption('1', 'b');
expect(options.eq(3)).toEqualOption('2', 'c');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to also have a test that verifies correct behaviour when arr is originally undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


describe('empty option', function() {

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