Angular2 Reactive Forms formControl for radio buttons

Mike Bovenlander picture Mike Bovenlander · Oct 10, 2016 · Viewed 72k times · Source

I'm building a form in Angular with Reactive Forms. I'm using the FormBuilder to make a group of fields. For textboxes this works extremely well. But I can't find a formControl for radio buttons.

How does this work? Should I use <input formControlName="gender" type="radio"> just like I use with text input's?

Answer

Paul Samsotha picture Paul Samsotha · Oct 10, 2016

Should I do <input formControlName="gender" type="radio"> just like I do with text input's?

Yes.

How does this work?

Form control directives use a ControlValueAccessor directive to communicate with the native element. There are different types of ControlValueAccessors for each of the possible inputs. The correct one is chosen by the selector of the value accessor directive. The selectors are based on what type the <input> is. When you have type="radio", then the value accessor for radios will be used.