Skip to content

Commit db993ba

Browse files
committed
fix(input): $render input even if $modelValue is empty
$render should only be concerned if there is a $viewValue present. When we check $isEmpty($modelValue), we assume $render is used in context of a model -> view update. But since it is part of the public API, it needs to work independent of internal usage. Fixes angular#9156
1 parent 9078a6a commit db993ba

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10291029
element.on('change', listener);
10301030

10311031
ctrl.$render = function() {
1032-
element.val(ctrl.$isEmpty(ctrl.$modelValue) ? '' : ctrl.$viewValue);
1032+
element.val(ctrl.$viewValue || '');
10331033
};
10341034
}
10351035

test/ng/directive/inputSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ describe('NgModelController', function() {
484484

485485
});
486486

487+
487488
describe('validations pipeline', function() {
488489

489490
it('should perform validations when $validate() is called', function() {
@@ -2075,6 +2076,21 @@ describe('input', function() {
20752076
expect(inputElm.val()).toBe('0');
20762077
});
20772078

2079+
it('should render the $viewValue when $modelValue is empty', function() {
2080+
compileInput('<input type="text" ng-model="value" />');
2081+
2082+
var ctrl = inputElm.controller('ngModel');
2083+
2084+
ctrl.$modelValue = null;
2085+
2086+
expect(ctrl.$isEmpty(ctrl.$modelValue)).toBe(true);
2087+
2088+
ctrl.$viewValue = 'abc';
2089+
ctrl.$render();
2090+
2091+
expect(inputElm.val()).toBe('abc');
2092+
});
2093+
20782094

20792095
describe('pattern', function() {
20802096

0 commit comments

Comments
 (0)