Search code examples
angularangular-reactive-formsangular-forms

Angular2 Reactive Forms formControl for radio buttons


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?


Solution

  • 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.