fix(input.radio): support 2-way binding in a repeater

Closes #869
This commit is contained in:
Vojta Jina 2012-04-10 13:41:51 -07:00
parent 5bcd719866
commit 93d62860e9
2 changed files with 51 additions and 5 deletions

View file

@ -576,8 +576,10 @@ function emailInputType(scope, element, attr, ctrl, $sniffer, $browser) {
} }
function radioInputType(scope, element, attr, ctrl) { function radioInputType(scope, element, attr, ctrl) {
// correct the name // make the name unique, if not defined
element.attr('name', attr.id + '@' + attr.name); if (isUndefined(attr.name)) {
element.attr('name', nextUid());
}
element.bind('click', function() { element.bind('click', function() {
if (element[0].checked) { if (element[0].checked) {
@ -1144,9 +1146,9 @@ var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
var ngValueDirective = [function() { var ngValueDirective = [function() {
return { return {
priority: 100, priority: 100,
compile: function(tpl, attr) { compile: function(tpl, tplAttr) {
if (CONSTANT_VALUE_REGEXP.test(attr.ngValue)) { if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
return function(scope) { return function(scope, elm, attr) {
attr.$set('value', scope.$eval(attr.ngValue)); attr.$set('value', scope.$eval(attr.ngValue));
}; };
} else { } else {

View file

@ -1106,5 +1106,49 @@ describe('input', function() {
browserTrigger(inputElm.eq(1), 'click'); browserTrigger(inputElm.eq(1), 'click');
expect(scope.selected).toBe(2); 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);
});
}); });
}); });