This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

RadioControlValueAccessor

Writes radio control values and listens to radio control changes.

See more...

NgModules

Selectors

Properties

Property Description
onChange: () => { }
onTouched: () => { }
@Input()
name: string
@Input()
formControlName: string
@Input()
value: any

Description

Used by NgModel, FormControlDirective, and FormControlName to keep the view synced with the FormControl model.

If you have imported the FormsModule or the ReactiveFormsModule, this value accessor will be active on any radio control that has a form directive. You do not need to add a special selector to activate it.

How to use radio buttons with form directives

To use radio buttons in a template-driven form, you'll want to ensure that radio buttons in the same group have the same name attribute. Radio buttons with different name attributes do not affect each other.

import {Component} from '@angular/core'; @Component({ selector: 'example-app', template: ` <form #f="ngForm"> <input type="radio" value="beef" name="food" [(ngModel)]="myFood"> Beef <input type="radio" value="lamb" name="food" [(ngModel)]="myFood"> Lamb <input type="radio" value="fish" name="food" [(ngModel)]="myFood"> Fish </form> <p>Form value: {{ f.value | json }}</p> <!-- {food: 'lamb' } --> <p>myFood value: {{ myFood }}</p> <!-- 'lamb' --> `, }) export class RadioButtonComp { myFood = 'lamb'; }
      
      
  1. import {Component} from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'example-app',
  5. template: `
  6. <form #f="ngForm">
  7. <input type="radio" value="beef" name="food" [(ngModel)]="myFood"> Beef
  8. <input type="radio" value="lamb" name="food" [(ngModel)]="myFood"> Lamb
  9. <input type="radio" value="fish" name="food" [(ngModel)]="myFood"> Fish
  10. </form>
  11. <p>Form value: {{ f.value | json }}</p> <!-- {food: 'lamb' } -->
  12. <p>myFood value: {{ myFood }}</p> <!-- 'lamb' -->
  13. `,
  14. })
  15. export class RadioButtonComp {
  16. myFood = 'lamb';
  17. }

When using radio buttons in a reactive form, radio buttons in the same group should have the same formControlName. You can also add a name attribute, but it's optional.

import {Component} from '@angular/core'; import {FormControl, FormGroup} from '@angular/forms'; @Component({ selector: 'example-app', template: ` <form [formGroup]="form"> <input type="radio" formControlName="food" value="beef" > Beef <input type="radio" formControlName="food" value="lamb"> Lamb <input type="radio" formControlName="food" value="fish"> Fish </form> <p>Form value: {{ form.value | json }}</p> <!-- {food: 'lamb' } --> `, }) export class ReactiveRadioButtonComp { form = new FormGroup({ food: new FormControl('lamb'), }); }
      
      
  1. import {Component} from '@angular/core';
  2. import {FormControl, FormGroup} from '@angular/forms';
  3.  
  4. @Component({
  5. selector: 'example-app',
  6. template: `
  7. <form [formGroup]="form">
  8. <input type="radio" formControlName="food" value="beef" > Beef
  9. <input type="radio" formControlName="food" value="lamb"> Lamb
  10. <input type="radio" formControlName="food" value="fish"> Fish
  11. </form>
  12. <p>Form value: {{ form.value | json }}</p> <!-- {food: 'lamb' } -->
  13. `,
  14. })
  15. export class ReactiveRadioButtonComp {
  16. form = new FormGroup({
  17. food: new FormControl('lamb'),
  18. });
  19. }

Methods

ngOnInit(): void
      
      ngOnInit(): void
    

Parameters

There are no parameters.

Returns

void

ngOnDestroy(): void
      
      ngOnDestroy(): void
    

Parameters

There are no parameters.

Returns

void

writeValue(value: any): void
      
      writeValue(value: any): void
    

Parameters

value

Type: any.

Returns

void

registerOnChange(fn: (_: any) => { }): void
      
      registerOnChange(fn: (_: any) => {
}): void
    

Parameters

fn

Type: (_: any) => { }.

Returns

void

fireUncheck(value: any): void
      
      fireUncheck(value: any): void
    

Parameters

value

Type: any.

Returns

void

registerOnTouched(fn: () => { }): void
      
      registerOnTouched(fn: () => {
}): void
    

Parameters

fn

Type: () => { }.

Returns

void

setDisabledState(isDisabled: boolean): void
      
      setDisabledState(isDisabled: boolean): void
    

Parameters

isDisabled

Type: boolean.

Returns

void