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

Commit 47724ba

Browse files
lgalfasoNarretz
authored andcommitted
feat(input[radio]): allow ng-trim to work for input[type=radio]
Allow input[type=radio] elements to specify ng-trim. Closes: #12091 Fixes: #11859
1 parent 305ba1a commit 47724ba

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/ng/directive/input.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,21 +1461,30 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
14611461
}
14621462

14631463
function radioInputType(scope, element, attr, ctrl) {
1464+
var doTrim = !attr.ngTrim || trim(attr.ngTrim) !== 'false';
14641465
// make the name unique, if not defined
14651466
if (isUndefined(attr.name)) {
14661467
element.attr('name', nextUid());
14671468
}
14681469

14691470
var listener = function(ev) {
1471+
var value;
14701472
if (element[0].checked) {
1471-
ctrl.$setViewValue(attr.value, ev && ev.type);
1473+
value = attr.value;
1474+
if (doTrim) {
1475+
value = trim(value);
1476+
}
1477+
ctrl.$setViewValue(value, ev && ev.type);
14721478
}
14731479
};
14741480

14751481
element.on('click', listener);
14761482

14771483
ctrl.$render = function() {
14781484
var value = attr.value;
1485+
if (doTrim) {
1486+
value = trim(value);
1487+
}
14791488
// Strict comparison would cause a BC
14801489
/* jshint eqeqeq:false */
14811490
element[0].checked = (value == ctrl.$viewValue);

test/ng/directive/inputSpec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,53 @@ describe('input', function() {
29892989
expect(inputElm[0].checked).toBe(false);
29902990
expect(inputElm[1].checked).toBe(false);
29912991
});
2992+
2993+
2994+
it('should allow the use of ngTrim', function() {
2995+
$rootScope.some = 11;
2996+
var inputElm = helper.compileInput(
2997+
'<input type="radio" ng-model="value" value="opt1" />' +
2998+
'<input type="radio" ng-model="value" value=" opt2 " />' +
2999+
'<input type="radio" ng-model="value" ng-trim="false" value=" opt3 " />' +
3000+
'<input type="radio" ng-model="value" ng-trim="false" value="{{some}}" />' +
3001+
'<input type="radio" ng-model="value" ng-trim="false" value=" {{some}} " />');
3002+
3003+
$rootScope.$apply(function() {
3004+
$rootScope.value = 'blue';
3005+
$rootScope.some = 'blue';
3006+
});
3007+
3008+
expect(inputElm[0].checked).toBe(false);
3009+
expect(inputElm[1].checked).toBe(false);
3010+
expect(inputElm[2].checked).toBe(false);
3011+
expect(inputElm[3].checked).toBe(true);
3012+
expect(inputElm[4].checked).toBe(false);
3013+
3014+
browserTrigger(inputElm[1], 'click');
3015+
expect($rootScope.value).toBe('opt2');
3016+
browserTrigger(inputElm[2], 'click');
3017+
expect($rootScope.value).toBe(' opt3 ');
3018+
browserTrigger(inputElm[3], 'click');
3019+
expect($rootScope.value).toBe('blue');
3020+
browserTrigger(inputElm[4], 'click');
3021+
expect($rootScope.value).toBe(' blue ');
3022+
3023+
$rootScope.$apply("value = ' opt2 '");
3024+
expect(inputElm[1].checked).toBe(false);
3025+
$rootScope.$apply("value = 'opt2'");
3026+
expect(inputElm[1].checked).toBe(true);
3027+
$rootScope.$apply("value = ' opt3 '");
3028+
expect(inputElm[2].checked).toBe(true);
3029+
$rootScope.$apply("value = 'opt3'");
3030+
expect(inputElm[2].checked).toBe(false);
3031+
3032+
$rootScope.$apply("value = 'blue'");
3033+
expect(inputElm[3].checked).toBe(true);
3034+
expect(inputElm[4].checked).toBe(false);
3035+
$rootScope.$apply("value = ' blue '");
3036+
expect(inputElm[3].checked).toBe(false);
3037+
expect(inputElm[4].checked).toBe(true);
3038+
});
29923039
});
29933040

29943041

0 commit comments

Comments
 (0)