From 93d62860e988a09fb64e594f50f6cd55a1fc5748 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Tue, 10 Apr 2012 13:41:51 -0700 Subject: [PATCH] fix(input.radio): support 2-way binding in a repeater Closes #869 --- src/ng/directive/input.js | 12 ++++++---- test/ng/directive/inputSpec.js | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 250a4520e025..dadf07c4c37b 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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) { @@ -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 { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 9f8b21c476a1..6cecf125e48f 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -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( + '
' + + '' + + '' + + '
'); + + 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( + '
' + + '' + + '' + + '
'); + + 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); + }); }); });