Skip to content

Commit 23d8474

Browse files
fix(input): trigger modelValue update on viewValue property changes
If the $viewValue is an object then we should trigger a model update if properties on that object change. Previously, we were only checking for change of object identity. Closes angular#7965
1 parent f07af61 commit 23d8474

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1794,10 +1794,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17941794
var viewValue = ctrl.$viewValue;
17951795

17961796
$timeout.cancel(pendingDebounce);
1797-
if (!revalidate && ctrl.$$lastCommittedViewValue === viewValue) {
1797+
if (!revalidate && equals(ctrl.$$lastCommittedViewValue, viewValue)) {
17981798
return;
17991799
}
1800-
ctrl.$$lastCommittedViewValue = viewValue;
1800+
ctrl.$$lastCommittedViewValue = copy(viewValue);
18011801

18021802
// change to dirty
18031803
if (ctrl.$pristine) {

test/ng/directive/inputSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,29 @@ describe('NgModelController', function() {
184184
});
185185

186186

187+
it('should trigger an update to the model if the $viewValue is an object whose properties change', function() {
188+
var log = [];
189+
190+
ctrl.$parsers.push(function(value) {
191+
log.push(value && value.getFullYear());
192+
return value;
193+
});
194+
195+
// assign a date object to the $viewValue
196+
var date = new Date(2000, 1, 1);
197+
ctrl.$setViewValue(date);
198+
expect(ctrl.$modelValue).toEqual(date);
199+
expect(log).toEqual([2000]);
200+
201+
// update the date value (not changing the object identity)
202+
log = [];
203+
date.setFullYear(2001);
204+
ctrl.$setViewValue(date);
205+
expect(ctrl.$modelValue).toEqual(date);
206+
expect(log).toEqual([2001]);
207+
});
208+
209+
187210
it('should fire viewChangeListeners when the value changes in the view (even if invalid)',
188211
function() {
189212
var spy = jasmine.createSpy('viewChangeListener');

0 commit comments

Comments
 (0)