ionic
  1. ionic-radio-button

Ionic Radio Button

Radio buttons are a common form element used in mobile apps to allow users to choose one option from a list of options. In Ionic, radio buttons can be easily implemented using the ion-radio component.

Syntax

<ion-list>
  <ion-radio-group [(ngModel)]="selectedOption">
    <ion-item>
      <ion-label>Option 1</ion-label>
      <ion-radio value="1"></ion-radio>
    </ion-item>
    <ion-item>
      <ion-label>Option 2</ion-label>
      <ion-radio value="2"></ion-radio>
    </ion-item>
    <ion-item>
      <ion-label>Option 3</ion-label>
      <ion-radio value="3"></ion-radio>
    </ion-item>
  </ion-radio-group>
</ion-list>

In this syntax, an ion-list contains an ion-radio-group that contains each ion-item with its corresponding ion-radio. The [(ngModel)] directive is used to bind the selected option to a variable in the component class.

Example

<ion-list>
  <ion-radio-group [(ngModel)]="selectedFruit">
    <ion-list-header>
      <ion-label>Fruits</ion-label>
    </ion-list-header>
    <ion-item>
      <ion-label>Apple</ion-label>
      <ion-radio value="apple"></ion-radio>
    </ion-item>
    <ion-item>
      <ion-label>Orange</ion-label>
      <ion-radio value="orange"></ion-radio>
    </ion-item>
    <ion-item>
      <ion-label>Banana</ion-label>
      <ion-radio value="banana"></ion-radio>
    </ion-item>
  </ion-radio-group>
</ion-list>

In this example, we are using an ion-radio-group to implement a list of fruit options, allowing the user to select one fruit.

Output

The output of this example is a list of radio buttons on the screen, and the selected fruit is bound to the selectedFruit variable in the component class.

Explanation

Radio buttons in Ionic are implemented using the ion-radio component. Each radio button should have a unique value. The ion-radio-group component groups the radio buttons together, and the [(ngModel)] directive is used to bind the selected option to a variable in the component class.

Use

Use the Ionic radio button component when you need to allow a user to select one option from a list of options. Radio buttons are commonly used in forms to gather data on user preferences.

Important Points

  • Radio buttons are a common form element used in mobile apps to allow users to choose one option from a list of options.
  • In Ionic, radio buttons are implemented using the ion-radio component.
  • Use the ion-radio-group component to group radio buttons together, and use the [(ngModel)] directive to bind the selected option to a variable in the component class.

Summary

Ionic radio buttons are an easy-to-use form element for allowing users to select one option from a list of options. The ion-radio component can be used to define each radio button, while the ion-radio-group component can be used to group them together. The [(ngModel)] directive can be used to bind the selected option to a variable in the component class.

Published on: