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

fix(ngOptions): use reference check only when not using trackBy #11996

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
3 changes: 1 addition & 2 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
// Check to see if the value has changed due to the update to the options
if (!ngModelCtrl.$isEmpty(previousValue)) {
var nextValue = selectCtrl.readValue();
if (ngOptions.trackBy && !equals(previousValue, nextValue) ||
previousValue !== nextValue) {
if (ngOptions.trackBy ? !equals(previousValue, nextValue) : previousValue !== nextValue) {
ngModelCtrl.$setViewValue(nextValue);
ngModelCtrl.$render();
}
Expand Down
20 changes: 20 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,26 @@ describe('ngOptions', function() {

});

it('should not set view value again if the tracked property of the model has not changed when using trackBy', function() {

createSelect({
'ng-model': 'selected',
'ng-options': 'item for item in arr track by item.id'
});

scope.$apply(function() {
scope.selected = {id: 10, label: 'ten'};
});

spyOn(element.controller('ngModel'), '$setViewValue');

scope.$apply(function() {
scope.arr[0] = {id: 10, label: 'ten'};
});

expect(element.controller('ngModel').$setViewValue).not.toHaveBeenCalled();
});

it('should not re-render if a property of the model is changed when not using trackBy', function() {

createSelect({
Expand Down