diff --git a/src/ng/directive/ngModel.js b/src/ng/directive/ngModel.js index da93dfd32c6a..77a43f910553 100644 --- a/src/ng/directive/ngModel.js +++ b/src/ng/directive/ngModel.js @@ -799,6 +799,36 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ } }; + function formatValue(modelValue) { + var formatters = ctrl.$formatters, + idx = formatters.length; + + var viewValue = modelValue; + while (idx--) { + viewValue = formatters[idx](viewValue); + } + + return viewValue; + } + + /** + * @ngdoc method + * @name ngModel.NgModelController#$setModelValue + * + */ + this.$setModelValue = function(modelValue) { + ctrl.$modelValue = ctrl.$$rawModelValue = modelValue; + + var viewValue = formatValue(this.$modelValue); + + if (this.$viewValue !== viewValue) { + this.$viewValue = ctrl.$$lastCommittedViewValue = viewValue; + this.$render(); + + ctrl.$$runValidators(undefined, modelValue, ctrl.$viewValue, noop); + } + }; + // model -> value // Note: we cannot use a normal scope.$watch as we want to detect the following: // 1. scope value is 'a' @@ -813,21 +843,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ // if scope model value and ngModel value are out of sync // TODO(perf): why not move this to the action fn? if (modelValue !== ctrl.$modelValue) { - ctrl.$modelValue = ctrl.$$rawModelValue = modelValue; - - var formatters = ctrl.$formatters, - idx = formatters.length; - - var viewValue = modelValue; - while (idx--) { - viewValue = formatters[idx](viewValue); - } - if (ctrl.$viewValue !== viewValue) { - ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue; - ctrl.$render(); - - ctrl.$$runValidators(undefined, modelValue, viewValue, noop); - } + ctrl.$setModelValue(modelValue); } return modelValue; diff --git a/test/ng/directive/ngModelSpec.js b/test/ng/directive/ngModelSpec.js index d23cb0298afc..3b69a5122954 100644 --- a/test/ng/directive/ngModelSpec.js +++ b/test/ng/directive/ngModelSpec.js @@ -455,6 +455,28 @@ describe('ngModel', function() { }); + describe('$setModelValue', function() { + + it('should set the value to $modelValue', function() { + ctrl.$setModelValue(10); + expect(ctrl.$modelValue).toBe(10); + }); + + it('should $render only if value changed', function() { + spyOn(ctrl, '$render'); + + ctrl.$setModelValue(3); + expect(ctrl.$render).toHaveBeenCalledOnce(); + ctrl.$render.reset(); + + ctrl.$formatters.push(function() {return 3;}); + ctrl.$setModelValue(5); + expect(ctrl.$render).not.toHaveBeenCalled(); + }); + + }); + + describe('model -> view', function() { it('should set the value to $modelValue', function() {