Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Ionic 2 Radio Button Programmatically using TypeScript

Lets say I have the following list:

<ion-list radio-group [(ngModel)]="autoManufacturers">
  <ion-list-header>
    Auto Manufacturers
  </ion-list-header>

  <ion-item>
    <ion-label>Cord</ion-label>
    <ion-radio value="cord"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>Duesenberg</ion-label>
    <ion-radio value="duesenberg"></ion-radio>
  </ion-item>

  <ion-item>
    <ion-label>Hudson</ion-label>
    <ion-radio value="hudson"></ion-radio>
  </ion-item>
</ion-list>

Depending upon what value I get from the database, I want to check the item in the list. Lets say if I get value 'cord' then item 2 should displayed as selected item. How can I achieve this using TypeScript?

like image 676
Prerak Tiwari Avatar asked Sep 19 '16 22:09

Prerak Tiwari


1 Answers

Since you're binding the radio group to the autoManufacturers property from your Component, the only thing you need to do is assigning to that property the value you get from the database:

// somewhere in your code
this.autoManufacturers = valueFromDataBase; // where valueFromDataBase === 'cord'

By doing that, the option with the cord value will be selected automatically.

like image 70
sebaferreras Avatar answered Oct 05 '22 23:10

sebaferreras