RadioButtonRadioButton is an extension to standard radio button element with skinning capabilities.

RadioButton - 图1

Documentation

Import

  1. import {RadioButtonModule} from 'primeng/radiobutton';
  2.  

Getting Started

Two-way value binding is defined using the standard ngModel directive.

  1. <p-radioButton name="groupname" value="val1" [(ngModel)]="selectedValue"></p-radioButton>
  2. <p-radioButton name="groupname" value="val2" [(ngModel)]="selectedValue"></p-radioButton>
  3.  
  1. export class ModelComponent {
  2. selectedValue: string;
  3. }
  4.  

As model is two-way binding enabled, giving a default value to the model is enough to display a radio button as checked by default.

  1. export class ModelComponent {
  2. selectedValue: string = 'val1';
  3. }
  4.  

Model Driven Forms

RadioButton can be used in a model driven form as well.

  1. <p-radioButton name="groupname" value="ps4" formControlName="console"></p-radioButton>
  2.  

Label

The label attribute provides a label text for the radio button. This label is also clickable and selects value.

  1. <p-radioButton name="groupname" value="val1" label="Option 2" [(ngModel)]="selectedValue"></p-radioButton>
  2.  

Properties

NameTypeDefaultDescription
namestringnullName of the radiobutton group.
valueanynullValue of the radiobutton.
labelstringnullLabel of the radiobutton.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
styleobjectnullInline style of the component.
styleClassstringnullStyle class of the component.
labelStyleClassstringnullStyle class of the label.

Events

NameParametersDescription
onClickevent: Click eventCallback to invoke on radio button click.
onFocusevent: Focus eventCallback to invoke when the radio button receives focus.
onBlurevent: Blur eventCallback to invoke when the radio button loses focus.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-radiobuttonContainer element
ui-radiobutton-boxContainer of icon.
ui-radiobutton-iconIcon element.
ui-chkbox-labelLabel element.
ui-label-activeLabel element of a checked state.
ui-label-focusLabel element of a focused state.
ui-label-disabledLabel element of a disabled state.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <div class="ui-g" style="width:250px;margin-bottom:10px">
  3. <div class="ui-g-12"><p-radioButton name="group1" value="Option 1" label="Option 1" [(ngModel)]="val1" inputId="opt1"></p-radioButton></div>
  4. <div class="ui-g-12"><p-radioButton name="group1" value="Option 2" label="Option 2" [(ngModel)]="val1" inputId="opt2"></p-radioButton></div>
  5. <div class="ui-g-12"><p-radioButton name="group1" value="Option 3" label="Option 3" [(ngModel)]="val1" inputId="opt3"></p-radioButton></div>
  6. </div>
  7. Selected Value = {{val1||'none'}}
  8. <h3>Preselection</h3>
  9. <div class="ui-g" style="width:250px;margin-bottom:10px">
  10. <div class="ui-g-12"><p-radioButton name="group2" value="Option 1" label="Option 1" [(ngModel)]="val2" inputId="preopt1"></p-radioButton></div>
  11. <div class="ui-g-12"><p-radioButton name="group2" value="Option 2" label="Option 2" [(ngModel)]="val2" inputId="preopt2"></p-radioButton></div>
  12. <div class="ui-g-12"><p-radioButton name="group2" value="Option 3" label="Option 3" [(ngModel)]="val2" inputId="preopt3"></p-radioButton></div>
  13. </div>
  14. Selected Value = {{val2||'none'}}
  15.  
  1. export class RadioButtonDemo {
  2. val1: string;
  3. val2: string = 'Option 2';
  4. }
  5.