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

Commit 8692f87

Browse files
Narretzpetebacondarwin
authored andcommitted
fix(input): set ngTrueValue on required checkbox
Fixes #5164
1 parent 40406e2 commit 8692f87

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/ng/directive/input.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1342,9 +1342,11 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
13421342
element[0].checked = ctrl.$viewValue;
13431343
};
13441344

1345-
// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
1345+
// Override the standard `$isEmpty` because the $viewValue of an empty checkbox is always set to `false`
1346+
// This is because of the parser below, which compares the `$modelValue` with `trueValue` to convert
1347+
// it to a boolean.
13461348
ctrl.$isEmpty = function(value) {
1347-
return value !== trueValue;
1349+
return value === false;
13481350
};
13491351

13501352
ctrl.$formatters.push(function(value) {

test/ng/directive/inputSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,22 @@ describe('input', function() {
22902290
});
22912291

22922292

2293+
it('should render the $viewValue when $modelValue is empty', function() {
2294+
compileInput('<input type="text" ng-model="value" />');
2295+
2296+
var ctrl = inputElm.controller('ngModel');
2297+
2298+
ctrl.$modelValue = null;
2299+
2300+
expect(ctrl.$isEmpty(ctrl.$modelValue)).toBe(true);
2301+
2302+
ctrl.$viewValue = 'abc';
2303+
ctrl.$render();
2304+
2305+
expect(inputElm.val()).toBe('abc');
2306+
});
2307+
2308+
22932309
describe('pattern', function() {
22942310

22952311
it('should validate in-lined pattern', function() {

0 commit comments

Comments
 (0)