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

Commit d003ab4

Browse files
committed
feat(ngModel): pass the previous and current model to view change listeners
1 parent ad4296d commit d003ab4

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/ng/directive/ngModel.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
547547
ctrl.$modelValue = allValid ? modelValue : undefined;
548548

549549
if (ctrl.$modelValue !== prevModelValue) {
550-
ctrl.$$writeModelToScope();
550+
ctrl.$$writeModelToScope(ctrl.$modelValue, prevModelValue);
551551
}
552552
}
553553
});
@@ -719,16 +719,16 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
719719

720720
function writeToModelIfNeeded() {
721721
if (ctrl.$modelValue !== prevModelValue) {
722-
ctrl.$$writeModelToScope();
722+
ctrl.$$writeModelToScope(ctrl.$modelValue, prevModelValue);
723723
}
724724
}
725725
};
726726

727-
this.$$writeModelToScope = function() {
727+
this.$$writeModelToScope = function(currentModelValue, previousModelValue) {
728728
ngModelSet($scope, ctrl.$modelValue);
729729
forEach(ctrl.$viewChangeListeners, function(listener) {
730730
try {
731-
listener();
731+
listener(currentModelValue, previousModelValue);
732732
} catch (e) {
733733
$exceptionHandler(e);
734734
}
@@ -751,7 +751,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
751751
* and `$validators` pipelines. If there are no special {@link ngModelOptions} specified then the staged
752752
* value sent directly for processing, finally to be applied to `$modelValue` and then the
753753
* **expression** specified in the `ng-model` attribute. Lastly, all the registered change listeners,
754-
* in the `$viewChangeListeners` list, are called.
754+
* in the `$viewChangeListeners` list, are called. The parameters for the $viewChangeListeners are
755+
* the current and the previous value.
755756
*
756757
* In case the {@link ng.directive:ngModelOptions ngModelOptions} directive is used with `updateOn`
757758
* and the `default` trigger is not listed, all those actions will remain pending until one of the

test/ng/directive/ngModelSpec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ describe('ngModel', function() {
229229
});
230230

231231

232+
it('should pass the previous view value to the change listener', function() {
233+
ctrl.$setViewValue('val');
234+
235+
var spy = jasmine.createSpy('viewChangeListener');
236+
ctrl.$viewChangeListeners.push(spy);
237+
ctrl.$setViewValue('val2');
238+
239+
expect(spy).toHaveBeenCalledWith('val2', 'val');
240+
});
241+
242+
232243
it('should reset the model when the view is invalid', function() {
233244
ctrl.$setViewValue('aaaa');
234245
expect(ctrl.$modelValue).toBe('aaaa');

0 commit comments

Comments
 (0)