From d49df66faa165f818409356dcf6d891cc15924c5 Mon Sep 17 00:00:00 2001 From: Kara Erickson Date: Mon, 27 Jun 2016 14:27:35 -0700 Subject: [PATCH] fix(forms): make radio button selection logic more flexible Closes #9558 --- .../radio_control_value_accessor.ts | 1 + .../@angular/forms/test/integration_spec.ts | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/modules/@angular/forms/src/directives/radio_control_value_accessor.ts b/modules/@angular/forms/src/directives/radio_control_value_accessor.ts index 5c73f26df337b..6e421e2c2f10c 100644 --- a/modules/@angular/forms/src/directives/radio_control_value_accessor.ts +++ b/modules/@angular/forms/src/directives/radio_control_value_accessor.ts @@ -51,6 +51,7 @@ export class RadioControlRegistry { private _isSameGroup( controlPair: [NgControl, RadioControlValueAccessor], accessor: RadioControlValueAccessor) { + if (!controlPair[0].control) return false; return controlPair[0].control.root === accessor._control.control.root && controlPair[1].name === accessor.name; } diff --git a/modules/@angular/forms/test/integration_spec.ts b/modules/@angular/forms/test/integration_spec.ts index 864386c696fe8..90ac1a2f32013 100644 --- a/modules/@angular/forms/test/integration_spec.ts +++ b/modules/@angular/forms/test/integration_spec.ts @@ -615,6 +615,45 @@ export function main() { }); })); + it('should support removing controls from ', + inject( + [TestComponentBuilder, AsyncTestCompleter], + (tcb: TestComponentBuilder, async: AsyncTestCompleter) => { + const t = ` + + +
+
+ + +
+
`; + + const ctrl = new FormControl('fish'); + const showRadio = new FormControl('yes'); + const form = new FormGroup({'food': ctrl}); + + tcb.overrideTemplate(MyComp8, t) + .overrideProviders(MyComp8, providerArr) + .createAsync(MyComp8) + .then((fixture) => { + fixture.debugElement.componentInstance.form = form; + fixture.debugElement.componentInstance.showRadio = showRadio; + showRadio.valueChanges.subscribe((change) => { + (change === 'yes') ? form.addControl('food', new FormControl('fish')) : + form.removeControl('food'); + }); + fixture.detectChanges(); + + const input = fixture.debugElement.query(By.css('[value="no"]')); + dispatchEvent(input.nativeElement, 'change'); + + fixture.detectChanges(); + expect(form.value).toEqual({}); + async.done(); + }); + })); + describe('should support