From ed0ade6f348354a095fc97cdd6634f594989a4f9 Mon Sep 17 00:00:00 2001 From: Kara Date: Mon, 27 Jun 2016 15:29:33 -0600 Subject: [PATCH] fix(forms): make radio button selection logic more flexible (#9646) 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 5c73f26df3..6e421e2c2f 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 864386c696..90ac1a2f32 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