From 23d84740c4ab86441a8bfee1313f6abb2318c66a Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 2 Jul 2014 13:11:57 +0100 Subject: [PATCH] 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 #7965 --- src/ng/directive/input.js | 4 ++-- test/ng/directive/inputSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 9ee879dc1890..e5ac7996af97 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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); // change to dirty if (ctrl.$pristine) { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index c4acdc05a679..66330ff61fb8 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -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');