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

fix(input): trigger modelValue update on viewValue property changes #8047

Closed
Closed
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
4 changes: 2 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
@@ -1794,10 +1794,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
var viewValue = ctrl.$viewValue;

$timeout.cancel(pendingDebounce);
if (!revalidate && ctrl.$$lastCommittedViewValue === viewValue) {
if (!revalidate && equals(ctrl.$$lastCommittedViewValue, viewValue)) {
return;
}
ctrl.$$lastCommittedViewValue = viewValue;
ctrl.$$lastCommittedViewValue = copy(viewValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the copy should be done as early as possible (in $setViewValue) and not here, otherwise updates to the object that was passed in might effect ctrl.$viewValue, ctrl.$modelValue and the scope bound to it.


// change to dirty
if (ctrl.$pristine) {
23 changes: 23 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
@@ -184,6 +184,29 @@ describe('NgModelController', function() {
});


it('should trigger an update to the model if the $viewValue is an object whose properties change', function() {
var log = [];

ctrl.$parsers.push(function(value) {
log.push(value && value.getFullYear());
return value;
});

// assign a date object to the $viewValue
var date = new Date(2000, 1, 1);
ctrl.$setViewValue(date);
expect(ctrl.$modelValue).toEqual(date);
expect(log).toEqual([2000]);

// update the date value (not changing the object identity)
log = [];
date.setFullYear(2001);
ctrl.$setViewValue(date);
expect(ctrl.$modelValue).toEqual(date);
expect(log).toEqual([2001]);
});


it('should fire viewChangeListeners when the value changes in the view (even if invalid)',
function() {
var spy = jasmine.createSpy('viewChangeListener');