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

feat(ngTrim): allow ng-trim to work for elements input[type=radio] #12091

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

attr = nAttrs[j];
name = attr.name;
value = trim(attr.value);
value = attr.value;

// support ngAttr attribute binding
ngAttrName = directiveNormalize(name);
Expand Down
11 changes: 10 additions & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,21 +1379,30 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}

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

var listener = function(ev) {
var value;
if (element[0].checked) {
ctrl.$setViewValue(attr.value, ev && ev.type);
value = attr.value;
if (doTrim) {
value = trim(value);
}
ctrl.$setViewValue(value, ev && ev.type);
}
};

element.on('click', listener);

ctrl.$render = function() {
var value = attr.value;
if (doTrim) {
value = trim(value);
}
element[0].checked = (value == ctrl.$viewValue);
};

Expand Down
47 changes: 47 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,53 @@ describe('input', function() {
expect(inputElm[0].checked).toBe(false);
expect(inputElm[1].checked).toBe(false);
});


it('should allow the use of ngTrim', function() {
$rootScope.some = 11;
var inputElm = helper.compileInput(
'<input type="radio" ng-model="value" value="opt1" />' +
'<input type="radio" ng-model="value" value=" opt2 " />' +
'<input type="radio" ng-model="value" ng-trim="false" value=" opt3 " />' +
'<input type="radio" ng-model="value" ng-trim="false" value="{{some}}" />' +
'<input type="radio" ng-model="value" ng-trim="false" value=" {{some}} " />');

$rootScope.$apply(function() {
$rootScope.value = 'blue';
$rootScope.some = 'blue';
});

expect(inputElm[0].checked).toBe(false);
expect(inputElm[1].checked).toBe(false);
expect(inputElm[2].checked).toBe(false);
expect(inputElm[3].checked).toBe(true);
expect(inputElm[4].checked).toBe(false);

browserTrigger(inputElm[1], 'click');
expect($rootScope.value).toBe('opt2');
browserTrigger(inputElm[2], 'click');
expect($rootScope.value).toBe(' opt3 ');
browserTrigger(inputElm[3], 'click');
expect($rootScope.value).toBe('blue');
browserTrigger(inputElm[4], 'click');
expect($rootScope.value).toBe(' blue ');

$rootScope.$apply("value = ' opt2 '");
expect(inputElm[1].checked).toBe(false);
$rootScope.$apply("value = 'opt2'");
expect(inputElm[1].checked).toBe(true);
$rootScope.$apply("value = ' opt3 '");
expect(inputElm[2].checked).toBe(true);
$rootScope.$apply("value = 'opt3'");
expect(inputElm[2].checked).toBe(false);

$rootScope.$apply("value = 'blue'");
expect(inputElm[3].checked).toBe(true);
expect(inputElm[4].checked).toBe(false);
$rootScope.$apply("value = ' blue '");
expect(inputElm[3].checked).toBe(false);
expect(inputElm[4].checked).toBe(true);
});
});


Expand Down