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

fix(select): keep ngModel when selected option is recreated with ngRe… #15632

Merged
merged 4 commits into from
Jan 26, 2017
Merged
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
2 changes: 1 addition & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ var SelectController =
var removeValue = optionAttrs.value;

self.removeOption(removeValue);
self.ngModelCtrl.$render();
scheduleRender();

if (self.multiple && currentValue && currentValue.indexOf(removeValue) !== -1 ||
currentValue === removeValue
Expand Down
34 changes: 34 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,40 @@ describe('select', function() {

});

it('should keep the ngModel value when the selected option is recreated by ngRepeat', function() {
scope.options = [{ name: 'A'}, { name: 'B'}, { name: 'C'}];
scope.obj = {
value: 'B'
};

compile(
'<select ng-model="obj.value">' +
'<option ng-repeat="option in options" value="{{option.name}}">{{option.name}}</option>' +
'</select>'
);

var optionElements = element.find('option');
expect(optionElements.length).toEqual(3);
expect(optionElements[0].value).toBe('A');
expect(optionElements[1]).toBeMarkedAsSelected();
expect(scope.obj.value).toBe('B');

scope.$apply(function() {
// Only when new objects are used, ngRepeat re-creates the element from scratch
Copy link
Member

Choose a reason for hiding this comment

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

Just to make it more explicit, I would add an expactation that optionElements[1] (before) !== optionElements[0] (after).

scope.options = [{ name: 'B'}, { name: 'C'}, { name: 'D'}];
});

var previouslySelectedOptionElement = optionElements[1];
optionElements = element.find('option');

Copy link
Member

Choose a reason for hiding this comment

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

Misplaced newline?

expect(optionElements.length).toEqual(3);
expect(optionElements[0].value).toBe('B');
expect(optionElements[0]).toBeMarkedAsSelected();
expect(scope.obj.value).toBe('B');
// Ensure the assumption that the element is re-created is true
expect(previouslySelectedOptionElement).not.toBe(optionElements[0]);
});

});


Expand Down