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

Commit

Permalink
fix(input.radio): support 2-way binding in a repeater
Browse files Browse the repository at this point in the history
Closes #869
  • Loading branch information
vojtajina committed Apr 11, 2012
1 parent 8e41027 commit a22e535
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
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);
});
});
});

0 comments on commit a22e535

Please sign in to comment.