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

fix(input.radio): make it work inside repeater #871

Merged
merged 1 commit into from
Apr 11, 2012
Merged
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
12 changes: 7 additions & 5 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
}

function radioInputType(scope, element, attr, ctrl) {
// correct the name
element.attr('name', attr.id + '@' + attr.name);
// make the name unique, if not defined
if (isUndefined(attr.name)) {
element.attr('name', nextUid());
}

element.bind('click', function() {
if (element[0].checked) {
Expand Down Expand Up @@ -1144,9 +1146,9 @@ var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
var ngValueDirective = [function() {
return {
priority: 100,
compile: function(tpl, attr) {
if (CONSTANT_VALUE_REGEXP.test(attr.ngValue)) {
return function(scope) {
compile: function(tpl, tplAttr) {
if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
return function(scope, elm, attr) {
attr.$set('value', scope.$eval(attr.ngValue));
};
} else {
Expand Down
44 changes: 44 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,5 +1106,49 @@ describe('input', function() {
browserTrigger(inputElm.eq(1), 'click');
expect(scope.selected).toBe(2);
});


it('should work inside ngRepeat with primitive values', function() {
compileInput(
'<div ng-repeat="i in items">' +
'<input type="radio" name="sel_{{i.id}}" ng-model="i.selected" ng-value="true">' +
'<input type="radio" name="sel_{{i.id}}" ng-model="i.selected" ng-value="false">' +
'</div>');

scope.$apply(function() {
scope.items = [{id: 1, selected: true}, {id: 2, selected: false}];
});

inputElm = formElm.find('input');
expect(inputElm[0].checked).toBe(true);
expect(inputElm[1].checked).toBe(false);
expect(inputElm[2].checked).toBe(false);
expect(inputElm[3].checked).toBe(true);

browserTrigger(inputElm.eq(1), 'click');
expect(scope.items[0].selected).toBe(false);
});


it('should work inside ngRepeat without name attribute', function() {
compileInput(
'<div ng-repeat="i in items">' +
'<input type="radio" ng-model="i.selected" ng-value="true">' +
'<input type="radio" ng-model="i.selected" ng-value="false">' +
'</div>');

scope.$apply(function() {
scope.items = [{id: 1, selected: true}, {id: 2, selected: false}];
});

inputElm = formElm.find('input');
expect(inputElm[0].checked).toBe(true);
expect(inputElm[1].checked).toBe(false);
expect(inputElm[2].checked).toBe(false);
expect(inputElm[3].checked).toBe(true);

browserTrigger(inputElm.eq(1), 'click');
expect(scope.items[0].selected).toBe(false);
});
});
});