Skip to content

Commit 187654c

Browse files
Narretzpetebacondarwin
authored andcommitted
fix(input): set ngTrueValue on required checkbox
Fixes angular#5164
1 parent 006b078 commit 187654c

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
@@ -1338,9 +1338,11 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
13381338
element[0].checked = ctrl.$viewValue;
13391339
};
13401340

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

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

test/ng/directive/inputSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,22 @@ describe('input', function() {
22552255
});
22562256

22572257

2258+
it('should render the $viewValue when $modelValue is empty', function() {
2259+
compileInput('<input type="text" ng-model="value" />');
2260+
2261+
var ctrl = inputElm.controller('ngModel');
2262+
2263+
ctrl.$modelValue = null;
2264+
2265+
expect(ctrl.$isEmpty(ctrl.$modelValue)).toBe(true);
2266+
2267+
ctrl.$viewValue = 'abc';
2268+
ctrl.$render();
2269+
2270+
expect(inputElm.val()).toBe('abc');
2271+
});
2272+
2273+
22582274
describe('pattern', function() {
22592275

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

0 commit comments

Comments
 (0)