From 332bab0d7d369ea9e7f2ef24c718fc527bfd12d6 Mon Sep 17 00:00:00 2001 From: Sriram Desikan Date: Tue, 11 Mar 2014 19:19:19 +0530 Subject: [PATCH] 5449-update-model-properties-when-directly-changed --- src/ng/directive/input.js | 14 +++++++++----- test/ng/directive/selectSpec.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index cb432c52a5c7..33ef7fc5d785 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1712,14 +1712,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ var ctrl = this; $scope.$watch(function ngModelWatch() { - var value = ngModelGet($scope); + var value = ngModelGet($scope), + formatters = ctrl.$formatters, + idx = formatters.length; // if scope model value and ngModel value are out of sync if (ctrl.$modelValue !== value) { - var formatters = ctrl.$formatters, - idx = formatters.length; - ctrl.$modelValue = value; while(idx--) { value = formatters[idx](value); @@ -1729,8 +1728,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ ctrl.$viewValue = value; ctrl.$render(); } - } + } else if ((value) && (value instanceof Array) && (value.length === 0)) { + // case where model is modifed directly, this helps to validate the change + while (idx--) { + value = formatters[idx](value); + } + } return value; }); }]; diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 6fcd1fe05f82..700bead51c3c 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -1331,5 +1331,15 @@ describe('select', function() { }).toThrowMinErr('ng','badname', 'hasOwnProperty is not a valid "option value" name'); }); + it('should become invalid if model length is set to zero in scope', function() { + scope.myselect = []; + compile(''); + scope.$apply (function() { + scope.myselect.length = 0; + }); + expect(element).toBeInvalid(); + }); + }); });